我有一个简单的多对多映射:
@Entity
@Table(name="ITEM")
public static class Item
{
@Id
@GeneratedValue
long id;
@Column
String name;
@ManyToMany(cascade={ CascadeType.ALL })
Set<Category> categories = new HashSet<Category> ();
}
@Entity
@Table(name="CATEGORY")
public static class Category
{
@Id
@GeneratedValue
long id;
@Column
String name;
}
而这个测试用例:
public void testPersist() throws Throwable
{
Category one = new Category ();
one.name = "one";
em.persist (one);
System.out.println ("one.id="+one.id);
Category two = new Category ();
two.name = "two";
em.persist (two);
System.out.println ("two.id="+two.id);
Item item = new Item ();
item.name = "item";
item.categories.add (one);
item.categories.add (two);
em.persist (item);
long id = item.id;
System.out.println ("item.id="+item.id);
System.out.println ("item.categories="+item.categories);
em.clear ();
item = em.find (Item.class, id);
System.out.println ("item.categories="+item.categories);
assertEquals (item.categories.toString (), 2, item.categories.size ());
}
测试失败。在日志中,我可以看到:
SchemaUpdate - create table CATEGORY (id bigint generated by default as identity (start with 1), name varchar(255), primary key (id))
SchemaUpdate - create table ITEM (id bigint generated by default as identity (start with 1), name varchar(255), primary key (id))
SchemaUpdate - create table ITEM_CATEGORY (ITEM_id bigint not null, categories_id bigint not null, primary key (ITEM_id, categories_id))
SchemaUpdate - alter table ITEM_CATEGORY add constraint FK5C7030AA7C924DF7 foreign key (categories_id) references CATEGORY
SchemaUpdate - alter table ITEM_CATEGORY add constraint FK5C7030AA66304535 foreign key (ITEM_id) references ITEM
所以创建了正确的表,双主键没问题,外键是正确的。
坚持item
不做正确的事情:
[DEBUG] org.hibernate.SQL - insert into CATEGORY (id, name) values (null, ?)
[TRACE] org.hibernate.type.StringType - binding 'one' to parameter: 1
[DEBUG] org.hibernate.SQL - call identity()
one.id=1
[DEBUG] org.hibernate.SQL - insert into CATEGORY (id, name) values (null, ?)
[TRACE] org.hibernate.type.StringType - binding 'two' to parameter: 1
[DEBUG] org.hibernate.SQL - call identity()
two.id=2
[DEBUG] org.hibernate.SQL - insert into ITEM (id, name) values (null, ?)
[TRACE] org.hibernate.type.StringType - binding 'item' to parameter: 1
[DEBUG] org.hibernate.SQL - call identity()
item.id=1
item.categories=[ch.globus.bonus.ManyToManyTest$Category@1d532ae, ch.globus.bonus.ManyToManyTest$Category@42a6eb]
如您所见,项目本身是持久化的,但不是包含类别的集合(连接表中的插入丢失)。但是集合中有值!
当它读取项目时,它会查询连接表:
[DEBUG] org.hibernate.SQL - select manytomany0_.id as id9_0_, manytomany0_.name as name9_0_ from ITEM manytomany0_ where manytomany0_.id=?
[TRACE] org.hibernate.type.LongType - binding '1' to parameter: 1
[TRACE] org.hibernate.type.StringType - returning 'item' as column: name9_0_
[DEBUG] org.hibernate.SQL - select categories0_.ITEM_id as ITEM1_1_, categories0_.categories_id as categories2_1_, manytomany1_.id as id10_0_, manytomany1_.name as name10_0_ from ITEM_CATEGORY categories0_ left outer join CATEGORY manytomany1_ on categories0_.categories_id=manytomany1_.id where categories0_.ITEM_id=?
[TRACE] org.hibernate.type.LongType - binding '1' to parameter: 1
item.categories=[]
但当然,它什么也找不到。怎么了?为什么休眠考虑连接表find()
而不考虑连接表persist()
?
我正在使用 Hibernate 3.3.1,Hibernate Annotations 3.4.0
[编辑] 为了解决这个问题,我尝试引入双向映射。首先,我将此代码添加到Category
:
/* BEGIN FIX1: made join bidrectional */
@ManyToMany(cascade={ CascadeType.ALL })
Set<Item> items = new HashSet<Item> ();
/* END FIX1: made join bidrectional */
然后,我更改了测试以更新双方:
item.categories.add (two);
/* BEGIN FIX1: made join bidrectional */
one.items.add (item);
two.items.add (item);
/* END FIX1: made join bidrectional */
em.persist (item);
/* BEGIN FIX1: made join bidrectional */
em.persist (one);
em.persist (two);
/* END FIX1: made join bidrectional */
我什至再次坚持分类。结果:什么都没有。Hibernate 很高兴地忽略了多对多映射。
作为第二个修复,我尝试按照@JoinTable
zoidbeck 的建议添加注释:
@ManyToMany(cascade={ CascadeType.ALL })
/* BEGIN FIX2: Added JoinTable */
@JoinTable(name = "ITEM_CATEGORY",
joinColumns={@JoinColumn(name="itm_id")},
inverseJoinColumns={@JoinColumn(name="cat_id")}
)
/* END FIX2: Added JoinTable */
Set<Category> categories = new HashSet<Category> ();
再次,什么都没有。