-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: @JoinTable on inherited entity
PostPosted: Wed Jan 30, 2013 4:45 am 
Newbie

Joined: Wed Jan 30, 2013 4:09 am
Posts: 1
[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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.