简体   繁体   English

休眠映射保存问题

[英]Hibernate mapping saving issue

I have a two problem entityes fields in my project: First: 我的项目中有两个问题实体字段:第一:

public class User implements UserDetails {
    private static final long serialVersionUID = -8442780593066407492L;
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "user_role",
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id")})
    private Set<UserRole> userRoles = new HashSet<UserRole>();

    @OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE)
    private Set<CommentReserv> comments = new HashSet<CommentReserv>();

    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> result = new 
        ArrayList<SimpleGrantedAuthority>();
        for (UserRole userRole: userRoles) {
            result.add(new 
            SimpleGrantedAuthority(userRole.getListRole().name()));
            }
        return result;
    }
...
}

Second: 第二:

public class CommentReserv implements Serializable {
    private static final long serialVersionUID = 1579363480188238317L;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @ManyToOne
    @JoinColumn(name = "reservation_id")
    private Reservation reservation;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id")
    private User user;
...
}

Third: 第三:

public class Reservation implements Serializable{
    private static final long serialVersionUID = -9007238193656173229L;

    @Id
    @Column(name = "id")
    private String id = generateId();
...
    @OneToMany(mappedBy = "reservation", cascade = CascadeType.REMOVE)
    private Set<CommentReserv> comments = new HashSet<CommentReserv>();
...

Then i get Reservation and User from DB... 然后我从数据库中获取预订和用户...

Reservation reservation = this.reservationRepository.getReservationById(params.get("reservationId").toString());            
User currentUser = this.userRepository.getUser(principal.getName());

(I checked it. User is loaded correctly.) And set it to new CommentReserv instance. (我检查了它。用户已正确加载。)并将其设置为新的CommentReserv实例。

commentReserv.setReservation(reservation);
commentReserv.setComment(params.get("comment").toString());
commentReserv.setUser(currentUser);

When i trying to save new CommentReserv, i get User copy in my DB. 当我尝试保存新的CommentReserv时,我在数据库中获得了用户副本。 I tryed to remove cascadetype from CommentReserv, but it produces TransientObjectException. 我试图从CommentReserv中删除级联类型,但是会产生TransientObjectException。 What is wrong with my code? 我的代码有什么问题? Second night without sleep... 第二夜没睡...

In my DB i have one user with id=0. 在我的数据库中,我有一个ID为= 0的用户。 It is strange, but hibernate assume that if user id = 0 then user is not exists in DB. 这很奇怪,但是休眠状态假设如果用户ID = 0,则该用户在数据库中不存在。 Really strange. 真的很奇怪 I just change id to 1 and it's work fine. 我只是将id更改为1,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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