繁体   English   中英

休眠@OneToMany或@ManyToOne关系对象不存在

[英]hibernate @OneToMany or @ManyToOne relation object not presist

你好,我有两个休眠实体类。

UserInfo ----->评论@OneToMany @ManyToOne

我的userinfo对象在数据库中仍然正确,但是注释表的userid条目未持久。

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();
    }
}

在数据库注释表userid中未插入.....

请指出错误并给出解决方案... :)

它与“传递持久性”有关。 持久化了主要实体“用户”后,就可以持久化与其父级联系的从属实体“ comm”。 怎么样? 您甚至不必显式保存注释实体。 只需保存“用户”,然后调用user.getList()。add(通信)并提交事务即可。 您可以但不必调用comm.save()。 传递性持久性为您做到了。 另一种保存“ comm”的方法是在session.save(comm)之前调用comm.setUser(用户)。 题外话:对变量名好一点:在UserInfo实体中设置的注释可以命名为“ comments”而不是“ list”。

暂无
暂无

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

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