Hibernate合并flush不更新数据

时间:2016-04-19 09:51:33

标签: java hibernate merge

我找到了一个解决方案,但没有什么对我有用,这是代码:

更新功能:

@Autowired
private SessionFactory sessionFactory;

...

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

实体(getter和setter ommited):

@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);

我尝试过使用update()函数和saveOrUpdate()但它不起作用,我不知道发生了什么。会话永远不会关闭,因此实体已附加。我已经检查过在我传递给updatePositionProfile()函数的对象中正确更改了值,但是当merge()时它什么也没做。

谢谢!

1 个答案:

答案 0 :(得分:0)

如果进行刷新,则PositionProfile仅适用于可见的同一会话。

如果select nr, sub_val, sub_nr, count(sub_val) as cnt, case when sub_val = 'A' then 0 when sum(count(case when sub_val <> 'A' then sub_val end)) -- <-- here over(partition by nr) >= 5 then count(sub_val) else 0 end as MoreThan5, case when sub_val = 'A' then count(sub_val) when sum(count(case when sub_val <> 'A' then sub_val end)) -- <-- and here over(partition by nr) < 5 then count(sub_val) else 0 end as LessThan5 from helptab group by nr, sub_val, sub_nr order by nr, sub_val, sub_nr 正常工作,您必须检查