Hibernate @ManyToOne relation with @JoinTable and inheritance

[Hibernate - 4.1.9.Final] [MySQL driver]

Hi, I have the following entities.

ANIMAL :

@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;

}

DOG :

@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;

}

With a cyclic relationship on Dog entity.

 @ManyToOne
 @JoinTable( name = "DOG_OTHER_DOG" )
 public Dog dog;

Next, I'm trying to persist a new dog using 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 );
 }

Schema is well created

Hibernate: create table 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

But finally, Hibernate always fails on the save method and throws.

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'dog_ANIMAL_ID' in 'field list'

Indeed, Hibernate don't use the join table to save the dog.

Hibernate: select sequence_next_hi_value from hibernate_sequences where sequence_name = 'ANIMAL' for update Hibernate: insert into hibernate_sequences(sequence_name, sequence_next_hi_value) values('ANIMAL', ?) Hibernate: update hibernate_sequences set sequence_next_hi_value = ? where sequence_next_hi_value = ? and sequence_name = 'ANIMAL' Hibernate: insert into DOG (ANIMAL_NAME, dog_ANIMAL_ID, ANIMAL_ID) values (?, ?, ?)

Well, my question is (:

  • Why did Hibernate do not use the join table ? (everything works fine if I delete Animal inheritance)
  • In my case, How can I solve the problem ?

Thanks in advance !


PS : I can't use @MappedSuperclass to solve this issue..

Answers


Need Your Help

Zend Framework - how to remove param from request?

zend-framework

How to completely remove param from request object inside controller?

can't access service reference of wcf service in wp7?

.net wcf windows-phone-7

i m developing wp7 application in which i have to take service reference of wcf sevice from website.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.