简体   繁体   中英

hibernate @OneToMany or @ManyToOne relation object not presist

hello i have two Hibernate Entity class.

UserInfo -----> Comments @OneToMany @ManyToOne

my userinfo object persist correct in database but comments table userid entry is not persist.

UserInfo.java
package com.app.pojo;

import java.sql.Blob;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.app.pojo.*;
@Entity
@Table(name="UserDetails")
public class UserInfo 
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="user_id")
    private int userid;
    private String firstname;
    private String lastname;
    private String username;
    private String password;
    private String email;
    private String country;
    private String url;
    @Lob
    private Blob profile_pic;
    private String last_login;

    @OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL,mappedBy="user")
    private Set<Comments> list;

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

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

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Blob getProfile_pic() {
        return profile_pic;
    }

    public void setProfile_pic(Blob profile_pic) {
        this.profile_pic = profile_pic;
    }

    public String getLast_login() {
        return last_login;
    }

    public void setLast_login(String last_login) {
        this.last_login = last_login;
    }

    public Set<Comments> getList() {
        return list;
    }

    public void setList(Set<Comments> list) {
        this.list = list;
    }

}

Comments.java

package com.app.pojo;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.app.pojo.*;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="Comments")
public class Comments 
{

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int commentid;
@ManyToOne
@JoinColumn(name="user_id",insertable=false, updatable=false, 
nullable=false)
private UserInfo user;

@Lob
private String commentcontent;

public int getCommentid() {
    return commentid;
}
public void setCommentid(int commentid) {
    this.commentid = commentid;
}

public String getCommentcontent() {
    return commentcontent;
}
public void setCommentcontent(String commentcontent) {
    this.commentcontent = commentcontent;
}
public UserInfo getUser() {
    return user;
}
public void setUser(UserInfo user) {
    this.user = user;
}   
}

Tester.java

package com.app.Tester;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.app.pojo.Comments;
import com.app.pojo.UserInfo;

public class Tester {

    public static void main(String[] args) 
    {
        UserInfo user = new UserInfo();

        user.setUsername("BugTest");
        user.setPassword("baba");

        Comments comm = new Comments();

        comm.setCommentcontent("hello this");


        SessionFactory factory = utils.HibernateUtils.getFactory();


        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();

        session.save(user); 

        session.save(comm);

        tx.commit();
    session.close();
    }
}

in database comments table userid is not inserted .....

please point out mistake and give solution ... :)

It has to do with "transitive persistence". Once you have persisted your main entity "user" you can persiste the dependent entity "comm" just conecting with his parent. How? You don't even have to explicitely save the comments entity. Just save "user" then call user.getList().add (comm) and commit the transaction. You can do, but don't need to, call comm.save(). Transitive persistence does it for you. Another way to save "comm" is call comm.setUser (user) before session.save(comm); Offtopic: be nice to the variable names: comments set in UserInfo entity may be named "comments" instead of "list".

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