2016-04-19 66 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();正常工作