使用注释一对多不添加列(休眠)
Using annotation one-to-many not add column(hibernate)
对不起我的英语。我有 2 tables - Posts(id, namePost, Text, idCat) 和 Category(id, nameCat)。我使用 hibernate
和模式 DAO
。我在 table Category 列 CategoryId
-> 到 Posts idCat
创建一对多。 Link 作品之王,输出信息不错。但是当我在 table Posts 中添加信息时,idCat 列没有添加,它在数据库中什么也没有。我无法在 table 帖子中添加此列 (idCat)。我尝试做了这么多 taime,但没有成功(
类别
@Entity
@Table(name="CATEGORY")
public class Category {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "ID", insertable=false, nullable=false)
private int id;
@Column(name="NAMECAT")
private String nameCat;
@Column(name="INDEX")
private boolean index;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "category")
private Set<Posts> post;
//then get and set
帖子
@Id @GeneratedValue
@Column(name = "ID", unique = true, nullable = false)
private int id;
@Column(name="NAMEPOST", nullable = false)
private String namePost;
@Column(name="TEXT", nullable = false)
private String text;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "IDCAT", insertable=false, updatable=false)
private Category category;
//code
接口 PostDao
public interface PostDao {
public void addPost(Posts post);
}
这是servlet
String[] catarrayid = request.getParameterValues("eachcat");//get cat what user use
String textpost = request.getParameter("textpost"); //text post
String namepost = request.getParameter("namepost"); //name post
if(!textpost.isEmpty() && !namepost.isEmpty() && catarrayid!=null) {
//open session
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Posts post = new Posts();
//this value take id category
int id = 0;
for(String s : catarrayid) {
//get id
id = Integer.parseInt(s);
}
//create post object
post.setnamePost(namepost);
post.setText(textpost);
//this i add in table post value idCat
//how add in object post category id?
//post.add(???);
try{
//and i add, bellow add method
Factory.getInstance().getPostDAO().addPost(post);
response.sendRedirect("indexadmin");
}catch(Exception e) {
System.out.println(e);
}finally{
if(session!=null && session.isOpen())
session.close();
}
像这样我添加方法
public void addPost(Posts post) {
Session session = null;
try{
session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(post);
session.getTransaction().commit();
} catch(Exception e) { outputError("addPost", e);
} finally{ closeSession(session); }
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "IDCAT", insertable=false, updatable=false)
private Category category;
为什么你有 insertable=false
?这基本上表示 Post 实体不负责 Category 实体上的插入操作。尝试删除 insertable=false
或将其更改为 insertable=true
(我认为这是默认设置)。
对不起我的英语。我有 2 tables - Posts(id, namePost, Text, idCat) 和 Category(id, nameCat)。我使用 hibernate
和模式 DAO
。我在 table Category 列 CategoryId
-> 到 Posts idCat
创建一对多。 Link 作品之王,输出信息不错。但是当我在 table Posts 中添加信息时,idCat 列没有添加,它在数据库中什么也没有。我无法在 table 帖子中添加此列 (idCat)。我尝试做了这么多 taime,但没有成功(
类别
@Entity
@Table(name="CATEGORY")
public class Category {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "ID", insertable=false, nullable=false)
private int id;
@Column(name="NAMECAT")
private String nameCat;
@Column(name="INDEX")
private boolean index;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "category")
private Set<Posts> post;
//then get and set
帖子
@Id @GeneratedValue
@Column(name = "ID", unique = true, nullable = false)
private int id;
@Column(name="NAMEPOST", nullable = false)
private String namePost;
@Column(name="TEXT", nullable = false)
private String text;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "IDCAT", insertable=false, updatable=false)
private Category category;
//code
接口 PostDao
public interface PostDao {
public void addPost(Posts post);
}
这是servlet
String[] catarrayid = request.getParameterValues("eachcat");//get cat what user use
String textpost = request.getParameter("textpost"); //text post
String namepost = request.getParameter("namepost"); //name post
if(!textpost.isEmpty() && !namepost.isEmpty() && catarrayid!=null) {
//open session
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Posts post = new Posts();
//this value take id category
int id = 0;
for(String s : catarrayid) {
//get id
id = Integer.parseInt(s);
}
//create post object
post.setnamePost(namepost);
post.setText(textpost);
//this i add in table post value idCat
//how add in object post category id?
//post.add(???);
try{
//and i add, bellow add method
Factory.getInstance().getPostDAO().addPost(post);
response.sendRedirect("indexadmin");
}catch(Exception e) {
System.out.println(e);
}finally{
if(session!=null && session.isOpen())
session.close();
}
像这样我添加方法
public void addPost(Posts post) {
Session session = null;
try{
session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(post);
session.getTransaction().commit();
} catch(Exception e) { outputError("addPost", e);
} finally{ closeSession(session); }
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "IDCAT", insertable=false, updatable=false)
private Category category;
为什么你有 insertable=false
?这基本上表示 Post 实体不负责 Category 实体上的插入操作。尝试删除 insertable=false
或将其更改为 insertable=true
(我认为这是默认设置)。