休眠多对多 - 交界处 table 为空
Hibernate many to many - junction table is empty
我想在 Java Hibernate 中建模多对多关系。
我希望图形有很多类别和类别分配给多个图形。
我的部分 java 类 代码:
@Entity
@Table(name = "graphic")
public class Graphic {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int graphicId;
@ManyToMany
private Set<Category> categories = new HashSet<Category>(0);
// ...
}
@Entity
@Table(name = "category")
public class category {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int categoryId;
// ...
}
我完成的步骤:
1. 创建了 Category
个实例。
2. 向数据库添加类别。
3. 创建了 Graphic
个实例。
4. 为图形集添加类别。
5. 向数据库添加图形。
结果:category
和graphic
实例中的记录正常。 Table graphic_category
已创建,但它是空的。结果连接 hql 查询没有返回任何行。
对于解决此问题的任何帮助,我将不胜感激。
编辑:代码步骤:
//In DBUtil
/**
* Gets category. If none exists creates new.
*/
public Category getCategory(String name) {
String hql = "From Category K where K.name = '" + name + "'";
Query query = session.createQuery(hql);
if (query.list().isEmpty()) {
return saveCategory(name);
} else {
return (Category)query.list().get(0);
}
}
public Category saveCategory(String name) {
Category kat = new Category(name);
session.save(kat);
return kat;
}
public Graphic saveGraphic(Graphic g) {
session.save(g);
return g;
}
//In unit test
Category kat1 = dbUtil.getCategory("kat1-g");
Graphic g1 = new Graphic();
g1.getCategories().add(kat1);
dbUtil.saveGraphic(g1);
我已将自动提交设置为 true。
使用 @ManyToMany
时,您必须指定哪个 table 将保持关系。
@ManyToMany
@JoinTable(name="graphic_category",joinColumns={@JoinColumn(name="graphicId")},inverseJoinColumns={@JoinColumn(name="categoryId")})
private Set<Category> categories;
希望对您有所帮助!
好的,我找到原因了。
在我的 Hibernate 配置文件中,我有:
<property name="connection.autocommit">true</property>
即使使用该设置,我也必须在将对象插入数据库后进行 session.flush()
以进行持久更改。
我想在 Java Hibernate 中建模多对多关系。
我希望图形有很多类别和类别分配给多个图形。
我的部分 java 类 代码:
@Entity
@Table(name = "graphic")
public class Graphic {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int graphicId;
@ManyToMany
private Set<Category> categories = new HashSet<Category>(0);
// ...
}
@Entity
@Table(name = "category")
public class category {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int categoryId;
// ...
}
我完成的步骤:
1. 创建了 Category
个实例。
2. 向数据库添加类别。
3. 创建了 Graphic
个实例。
4. 为图形集添加类别。
5. 向数据库添加图形。
结果:category
和graphic
实例中的记录正常。 Table graphic_category
已创建,但它是空的。结果连接 hql 查询没有返回任何行。
对于解决此问题的任何帮助,我将不胜感激。
编辑:代码步骤:
//In DBUtil
/**
* Gets category. If none exists creates new.
*/
public Category getCategory(String name) {
String hql = "From Category K where K.name = '" + name + "'";
Query query = session.createQuery(hql);
if (query.list().isEmpty()) {
return saveCategory(name);
} else {
return (Category)query.list().get(0);
}
}
public Category saveCategory(String name) {
Category kat = new Category(name);
session.save(kat);
return kat;
}
public Graphic saveGraphic(Graphic g) {
session.save(g);
return g;
}
//In unit test
Category kat1 = dbUtil.getCategory("kat1-g");
Graphic g1 = new Graphic();
g1.getCategories().add(kat1);
dbUtil.saveGraphic(g1);
我已将自动提交设置为 true。
使用 @ManyToMany
时,您必须指定哪个 table 将保持关系。
@ManyToMany
@JoinTable(name="graphic_category",joinColumns={@JoinColumn(name="graphicId")},inverseJoinColumns={@JoinColumn(name="categoryId")})
private Set<Category> categories;
希望对您有所帮助!
好的,我找到原因了。
在我的 Hibernate 配置文件中,我有:
<property name="connection.autocommit">true</property>
即使使用该设置,我也必须在将对象插入数据库后进行 session.flush()
以进行持久更改。