简体   繁体   中英

Hibernate merge with flush not updating data

I've finding a solution but nothing works for me, here's the code:

Update function:

@Autowired
private SessionFactory sessionFactory;

...

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

Entity (getters and 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 = " ";
    }
}

Controller (summarized for brevity):

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

I've tryed with update() function and saveOrUpdate() but it doesn't work, I don't know what's happening. Session is never closed so the entity is attached. I've checked that values are changed correctly in the object I passed to updatePositionProfile() function, but when merge() it simply does nothing.

Thanks!

if you make flush, the PositionProfile is only for the same session visible.

You must check, if the sessionFactory.getCurrentSession(); working correctly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM