0

SOLVED

But the real problem wasn't hibernate, it was mainly me and second Netbeans.

I was making a post, and saving my data. But I was making a duplicate post in js, and Netbeans won't fire breakpoints twice (and I don't really know why). So, for me, it was making one post and just one insert. But no, two posts, and two inserts.

Sorry for losing your time :/


I'm new to Hibernate, so I'm having some problems, maybe it's just a silly one, I don't really know.

I'm trying to insert an object to database with a @ManyToOne relationship, but Hibernate is duplicating it when I persist and commit the transaction. This is my code.

User Post Class

@Entity
@Table(name = "USERPOST", schema = "ADMIN1")
public class UserPost implements java.io.Serializable {

    private int iduserpost;    
    private String detail;
    private User user;

    public UserPost() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "IDUSERPOST", unique = true, nullable = false)
    public int getIduserpost() {
        return this.iduserpost;
    }

    public void setIduserpost(int iduserpost) {
        this.iduserpost = iduserpost;
    }
    @Column(name = "DETAIL", nullable = false)
    public String getDetail() {
        return this.detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    @ManyToOne
    @JoinColumn(name = "IDUSER")
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

User Class

@Entity
@Table(name = "USER", schema = "ADMIN1")
public class User implements java.io.Serializable {

    private int iduser;
    private String name;
    private String email;
    private Set<UserPost> posts;

    public User() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "IDUSER", unique = true, nullable = false)
    public int getIduser() {
        return this.iduser;
    }

    public void setIduser(int iduser) {
        this.iduser = iduser;
    }

    @Column(name = "NAME", nullable = false)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "EMAIL", nullable = false)
    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @OneToMany(mappedBy = "user")
    public Set<UserPost> getPosts() {
        return posts;
    }

    public void setPosts(Set<UserPost> posts) {
        this.posts = posts;
    }

}

Insert Method on Manager

public void Insert(Object data) {
    Session session = null;
    try {
        session = hibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.persist(data);
        session.getTransaction().commit();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        throw e;
    } finally {
        if (session.isOpen()) {
            session.close();
        }
    }
}

Insert on Servlet

FeedManager manager = new FeedManager();
UserPost post = new UserPost();
post.setDetail(text);
User usrPesist = (User) manager.GetById(User.class, idUser);
post.setUser(usrPesist);
manager.Insert(post);

The result I'm getting is TWO UserPosts being inserted to database, when what I want is just one.

Is there something wrong?

Ricardo Melo
  • 430
  • 1
  • 4
  • 12

3 Answers3

0

Since you have defined as private Set<UserPost> posts; in User Class, implement equals and hashcode methods in UserPost class , in that way you ensure that since it is a Set it wont add duplicates

But this is not a optimal way though.

shankarsh15
  • 1,947
  • 1
  • 11
  • 16
0

I think this is problem with you id generation strategy in your user class. Just try by setting id manually in user class object it will work.

Noman Akhtar
  • 690
  • 1
  • 12
  • 17
-1

Try changing your code with this :

@org.hibernate.annotations.Cascade( {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@OneToMany(mappedBy = "user")
@JoinColumn(name = "IDUSER")
public Set<UserPost> getPosts() {
    return posts;
}
Ruudy Garcia
  • 139
  • 9