你好我有两个Hibernate实体类。
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();
}
}
数据库注释表中的未插入用户ID .....
请指出错误并给出解决方案...... :)
答案 0 :(得分:0)
它与&#34;传递持久性&#34;有关。一旦你坚持你的主要实体&#34;用户&#34;你可以持久化依赖实体&#34; comm&#34;只是与他的父母联系。怎么样?您甚至不必明确保存评论实体。只需保存&#34;用户&#34;然后调用user.getList()。add(comm)并提交事务。您可以这样做,但不需要调用comm.save()。传递性持久性为您做到了。 另一种节省&#34; comm&#34;在session.save(comm)之前调用comm.setUser(user); Offtopic:对变量名称很好:在UserInfo实体中设置的注释可以命名为&#34; comments&#34;而不是&#34; list&#34;。