-->
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.  [ 2 posts ] 
Author Message
 Post subject: Inheritance mapping throwing one-to-many off
PostPosted: Thu Apr 20, 2006 2:30 pm 
Newbie

Joined: Fri May 21, 2004 3:25 pm
Posts: 8
Location: Los Angeles
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.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 30, 2006 6:29 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
No, not a bug, merely an area of confusion. Just because classes are related in java does not necessarily mean that they should be related in your hibernate mappings. And seeing as you hope to refer directly to subclasses in your mapping (Owner.getPlannerEvents()), you should (probably) not be mapping the inheritance relationship.

Try to rewrite your mapping without any inheritance. Keep the java inheritance, but completely ignore TimedEvent in your hibernate mapping, and refer directly to either CalendarEvent or PlannerEvent, as appropriate.

_________________
Code tags are your friend. Know them and use them.


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

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.