Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate annotations version:3.1 beta9
Mapping documents:
@Entity
@Table(name = "organizer_plannerevent")
@PrimaryKeyJoinColumn(name = "organizer_timedevent_id")
public class PlannerEvent extends TimedEvent {
}
-----------
@Entity
@Table(name = "organizer_calendarevent")
@PrimaryKeyJoinColumn(name = "organizer_timedevent_id")
public class CalendarEvent extends TimedEvent {
}
---------------------
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name = "organizer_timedevent")
public class TimedEvent extends PersistentEntity {
............
}
------------------------
class Owner{
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(
name="organizer_owner_link_organizer_plannerevent",
joinColumns = { @JoinColumn( name="organizer_owner_id") },
inverseJoinColumns = @JoinColumn( name="organizer_plannerevent_id")
)
public Set<PlannerEvent> getPlannerEvents() {
return plannerEvents;
}
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(
name="organizer_owner_link_organizer_calendarevent",
joinColumns = { @JoinColumn( name="organizer_owner_id") },
inverseJoinColumns = @JoinColumn( name="organizer_calendarevent_id")
)
.......
}
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
insert into organizer_owner (organizer_personalcontact_id) values (1)
insert into organizer_timedevent (organizer_eventtype_id, starts_at, ends_at) values (1, '2006-04-20 11:33:45.846', '2006-04-20 11:33:45.846')
insert into organizer_plannerevent (organizer_timedevent_id) values (1)
insert into organizer_timedevent (organizer_eventtype_id, starts_at, ends_at) values (2, '2006-04-20 11:33:45.846', '2006-04-20 11:33:45.846')
insert into organizer_calendarevent (organizer_timedevent_id) values (2)
insert into organizer_owner_link_organizer_plannerevent (organizer_owner_id, organizer_plannerevent_id) values (1, 1)
insert into organizer_owner_link_organizer_calendarevent (organizer_owner_id, organizer_calendarevent_id) values (1, 2)
WARN: SQL Error: 1452, SQLState: 23000 at org.hibernate.util.JDBCExceptionReporter.(JDBCExceptionReporter.java:71)
2969 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1452, SQLState: 23000
DWR-RROR: null, message from server: "Cannot add or update a child row: a foreign key constraint fails (`mysql/organizer_owner_link_organizer_calendarevent`, CONSTRAINT `fk_owner_calevent_2` FOREIGN KEY (`organizer_calendarevent_id`) REFERENCES `organizer_calendarevent` (`id`))" at org.hibernate.util.JDBCExceptionReporter.(JDBCExceptionReporter.java:72)
2969 [main] ERROR org.hibernate.util.JDBCExceptionReporter - null, message from server: "Cannot add or update a child row: a foreign key constraint fails (`mysql/organizer_owner_link_organizer_calendarevent`, CONSTRAINT `fk_owner_calevent_2` FOREIGN KEY (`organizer_calendarevent_id`) REFERENCES `organizer_calendarevent` (`id`))"
RROR: Could not synchronize database state with session at org.hibernate.event.def.AbstractFlushingEventListener.(AbstractFlushingEventListener.java:299)
MySQL
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
This is problem:
In the above mappings the class Owner has onetomany relations with 2 sibling classes (CalendarEvent and PlannerEvent, which extend TimedEvent).
When I try and persist the Owner, it always fails at the second onetomany mapping because it is trying to insert the primary key of the table timedevent into the child foreign key.
All the Ids are being generated using
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
using the auto-increment feature of mysql.
If you look at the sql:
There are 2 inserts into the timedevent table one for the calendar event and one for the planner event.
But the inserts into the link tables are using timedevent primary key instead of the subclass primary key.
Is this a bug ?
Any help is appreciated.
Thanks,
Vamsi Putrevu.