Spring Data JPA持久对象不更新

时间:2014-02-17 04:22:21

标签: 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)
    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;

如果我创建一个入学并保存,一切都很顺利。如果我然后检索Admission将位置更新为新的Location对象,则Admission不会保存/更新。我用来保存的代码是:

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;
    }
}

1 个答案:

答案 0 :(得分:2)

似乎

public void transfer(MSH msh, PV1 pv1, PV2 pv2) {

未在交易中执行,请将 @Transactional 添加到您的服务类。