[Hibernate - 4.1.9.Final] [MySQL 驱动程序]
嗨,我有以下实体。
动物 :
@Entity
@Table( name = "ANIMAL" )
@Inheritance( strategy = InheritanceType.TABLE_PER_CLASS )
public abstract class Animal implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy = GenerationType.TABLE )
@Column( name = "ANIMAL_ID" )
public Long id;
@Column( name = "ANIMAL_NAME" )
public String name;
}
狗 :
@Entity
@Table( name = "DOG" )
public class Dog extends Animal implements Cloneable, Serializable
{
private static final long serialVersionUID = 1L;
@ManyToOne
@JoinTable( name = "DOG_OTHER_DOG" )
public Dog dog;
}
在 Dog 实体上具有循环关系。
@ManyToOne
@JoinTable( name = "DOG_OTHER_DOG" )
public Dog dog;
接下来,我正在尝试使用 Hibernate Session API 来持久化一只新狗。
@Transactional
public void testAnimalJoinTable()
{
Dog dogFirst = new Dog();
dogFirst.name = "FirstDog";
Session session = sessionFactory.getCurrentSession();
session.save( dogFirst );
Dog dogSecond = new Dog();
dogSecond.dog = dogFirst;
dogSecond.name = "SecondDog";
session.save( dogSecond );
}
架构创建良好
Hibernate: 创建表 DOG (ANIMAL_ID bigint not null, ANIMAL_NAME varchar(255), primary key (ANIMAL_ID)) ENGINE=InnoDB
Hibernate: create table DOG_OTHER_DOG (dog_ANIMAL_ID bigint, ANIMAL_ID bigint not null, primary key (ANIMAL_ID)) ENGINE=InnoDB
但最后,Hibernate 总是在 save 方法上失败并抛出异常。
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“字段列表”中的未知列“dog_ANIMAL_ID”
事实上,Hibernate 不使用连接表来拯救狗。
Hibernate:从 hibernate_sequences 中选择 sequence_next_hi_value,其中 sequence_name = 'ANIMAL' 进行更新
Hibernate:插入 hibernate_sequences(sequence_name, sequence_next_hi_value) values('ANIMAL', ?)
Hibernate: update hibernate_sequences set sequence_next_hi_value = ? 其中 sequence_next_hi_value = ?和 sequence_name = 'ANIMAL'
Hibernate: 插入 DOG (ANIMAL_NAME, dog_ANIMAL_ID, ANIMAL_ID) 值 (?, ?, ?)
好吧,我的问题是(:
- 为什么 Hibernate 不使用连接表?(如果我删除动物继承一切正常)
- 就我而言,我该如何解决这个问题?
提前致谢 !
PS:我不能使用@MappedSuperclass 来解决这个问题..