[Hibernate 4.1.9.Final]
[MySQL connector 5.1.18]
Hi there,
I think we discovered an error on Hibernate.
Here are the steps to reproduce my problem :
VEHICLE :
Code:
@Entity
@Table( name = "VEHICLE" )
@Inheritance( strategy = InheritanceType.TABLE_PER_CLASS )
public abstract class Vehicle implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy = GenerationType.TABLE )
@Column( name = "VEHICLE_ID" )
public Long id;
@Column( name = "VEHICLE_NAME" )
public String name;
}
CAR :
Code:
@Entity
@Table( name = "CAR" )
public class Car extends Vehicle implements Cloneable, Serializable
{
private static final long serialVersionUID = 1L;
@ManyToOne
@JoinTable( name = "CAR_OTHER_CAR" )
public Car car;
}
With a cyclic relationship on 'Car' entity.
Code:
@ManyToOne
@JoinTable( name = "CAR_OTHER_CAR" )
public Car car;
Next, I'm trying to persist a new car using Hibernate Session API.
Code:
@Transactional
public void testVehicleJoinTable()
{
Car carFirst = new Car();
carFirst.name = "FirstCar";
Session session = sessionFactory.getCurrentSession();
session.save( carFirst );
Car carSecond = new Car();
carSecond.car = carFirst;
carSecond.name = "SecondCar";
session.save( carSecond );
}
Schema is well created
Code:
Hibernate: create table CAR (VEHICLE_ID bigint not null, VEHICLE_NAME varchar(255), primary key (VEHICLE_ID)) ENGINE=InnoDB
Hibernate: create table CAR_OTHER_CAR (car_VEHICLE_ID bigint, VEHICLE_ID bigint not null, primary key (VEHICLE_ID)) ENGINE=InnoDB
But finally, Hibernate always fails on the save method and throws.
Code:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'car_VEHICLE_ID' in 'field list'
Indeed, Hibernate don't use the join table to save the car.
Code:
Hibernate: select sequence_next_hi_value from hibernate_sequences where sequence_name = 'VEHICLE' for update
Hibernate: insert into hibernate_sequences(sequence_name, sequence_next_hi_value) values('VEHICLE', ?)
Hibernate: update hibernate_sequences set sequence_next_hi_value = ? where sequence_next_hi_value = ? and sequence_name = 'VEHICLE'
Hibernate: insert into CAR (VEHICLE_NAME, car_VEHICLE_ID, VEHICLE_ID) values (?, ?, ?)
Why did Hibernate do not use the join table ? (everything works fine if I delete 'Vehicle' inheritance)Thanks