2016-04-19 67 views
0

我已經找到一個解決方案,但沒有對我的作品合併,這裏是代碼:休眠平齊沒有更新數據

更新功能:

@Autowired 
private SessionFactory sessionFactory; 

... 

public void updatePositionProfile(PositionProfile positionProfile) { 
    Session session = sessionFactory.getCurrentSession(); 
    session.merge(positionProfile); 
    session.flush(); 
} 

實體(getter和setter方法中省略):

@Entity 
@Table(name = "position_profile") 
public class PositionProfile implements Serializable { 

    @Embeddable 
    public static class PositionProfile_PK implements Serializable { 

     private static final long serialVersionUID = 1L; 

     @NotNull 
     @Column(name="id_position") 
     Integer id_position; 

     @NotNull 
     @Column(name="profile") 
     String profile; 

     @NotNull 
     @Column(name="line") 
     String line; 

     PositionProfile_PK(){ 
      this.id_position = 0; 
      this.profile = new String(); 
      this.line = ""; 
     } 
    } 

    @Id 
    PositionProfile_PK positionProfilePK; 

    @NotNull 
    @Column(name="MAX_SPEED") 
    private Integer max_speed; 

    @NotNull 
    @Column(name="WARNING_SPEED") 
    private Integer warning_speed; 

    @NotNull 
    @Column(name="EMERGENCY_SPEED") 
    private Integer emergency_speed; 

    @NotNull 
    @Column(name="DISABLED") 
    private String disabled; 

    PositionProfile(){ 
     super(); 
     this.positionProfilePK = new PositionProfile_PK(); 
     this.max_speed = 0; 
     this.warning_speed = 0; 
     this.emergency_speed = 0; 
     this.disabled = " "; 
    } 
} 

控制器(概括爲簡潔起見):

PositionProfile positionProfileToUpdate = positionProfile.getPositionProfileByIdPositionAndProfile(pk, profile); 
positionProfileToUpdate.setMax_speed(ms); 
positionProfile.updatePositionProfile(positionProfileToUpdate); 

我試過更新()函數和saveOrUpdate(),但它不起作用,我不知道發生了什麼。會話永遠不會關閉,因此實體已連接。我已經檢查過,在傳遞給updatePositionProfile()函數的對象中值正確更改,但merge()時它什麼也不做。

謝謝!

回答

0

如果您進行刷新,則PositionProfile僅用於可見的同一會話。

您必須檢查,如果sessionFactory.getCurrentSession();正常工作