Spring数据JPA持久对象未更新
我在更新持久对象时遇到问题。我有两个对象“入场”和“地点”,如下所示:Spring数据JPA持久对象未更新,jpa,eclipselink,spring-data,Jpa,Eclipselink,Spring Data,我在更新持久对象时遇到问题。我有两个对象“入场”和“地点”,如下所示: @Entity @Table(name = "Admissions") public class Admission implements java.io.Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) pri
@Entity
@Table(name = "Admissions")
public class Admission implements java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(optional=false)
@JoinColumn(name="DemographicId", unique = true, nullable=false)
private Demographic demographic;
@OneToOne(optional=false)
@JoinColumn(name="VisitId", unique = true, nullable=false)
private Visit visit;
@OneToOne(optional=false)
@JoinColumn(name="LocationId", nullable=false)
private Location location;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "Updated", unique = false, nullable = false)
private Date updated;
@Entity
@Table(name = "Locations")
public class Location implements java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@JoinColumn(name="FacilityId", nullable=true)
private Facility facility;
@JoinColumn(name="WardId", nullable=true)
private Ward ward;
@JoinColumn(name="RoomId", nullable=true)
private Room room;
@JoinColumn(name="BedId", nullable=true)
private Bed bed;
如果我创建一个准入并保存它,一切都很好。如果我随后检索许可,将位置更新为新的位置对象,则许可不会保存/更新。我用来保存的代码是:
public void transfer(MSH msh, PV1 pv1, PV2 pv2) {
String visitNumber = pv1.getVisitNumber().getIDNumber().getValue();
Visit myVisit = visitService.findByVisitNumber(visitNumber);
if (myVisit == null) {
log.debug("Unable to transfer if we have no Visit");
return;
}
Admission myAdmission = admissionService.findByVisit(myVisit);
myAdmission.setLocation(getLocation(msh, pv1));
}
我已经尝试过进行最后的保存和刷新(这似乎不起作用),但我知道这不应该是必要的
我的服务类的一个例子
@Service
public class LocationServiceImpl implements LocationService {
@Resource
private LocationRepository repository;
@Transactional
public Location create(Location location) {
Location created = location;
return unique(created);
}
@Transactional
public Location findByLocation(Location location) {
return repository.findByLocation(location.getFacility(),
location.getWard(), location.getRoom(), location.getBed());
}
@Transactional
private Location unique(Location location) {
Location found = findByLocation(location);
if (found == null) {
return repository.save(location);
}
return found;
}
}
看来
public void transfer(MSH msh, PV1 pv1, PV2 pv2) {
未在事务中执行,请将@Transactional添加到您的服务类。谢谢。你一下子就解决了一大堆问题!总之,任何更新数据对象的方法都需要设置为@Transactional-正确吗?这是正确的,我的朋友,一旦方法end transaction被提交,但您必须有一个打开的有效事务来更新数据。如果您只是在搜索,则不需要交易记录