-->
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: Advanced Parent/Child Problem
PostPosted: Thu Nov 04, 2010 9:28 am 
Newbie

Joined: Fri Jan 26, 2007 9:58 am
Posts: 18
Location: Dublin
Hi All,

We have some pretty awkward mapping requirements on the project I'm working on that despite many hours searching and playing with Hibernate I'm not able to solve.

The issues are:

1. Every domain object needs a GUID
2. The GUID needs to be able to used in isolation to return the domain object
3. The GUID needs to be generated by an offset sequence (ie. node 1 Guids would be 1,51,101 node2: 2,52,102 etc)
4. The GUID needs to be the PK for each domain entity (hence not using UUIDs etc)

Ideally, we'd like each GUID to be an ID,Name combination where the ID gives us the PK for the domain entity and the string is a pointer back to the entitiy type, e.g. 51:com.here.Person, such that we can then use this to go to the Person table and get object 51.

To do this, the GUID has to be a mandatory, OneToOne, unidirectional, generated PK for each Domain entity:

Person:
======
@ID
@OneToOne
Guid guid;

Guid:
=====

@Id
long id;

String name;

The problem we're having is making this association work, as everytime we insert a person, we need to create a GUID first. let the DB generate the Key, and then copy its PK into the Person before submitting that. I have got it to the stage where hibernate is happy but on submission to the DB we get a FK violation.

Our mappings are below:

Person:
Code:
public class Person {

        /* Other stuff cut */
   
   @SuppressWarnings("unused")
   @Id
   @Column(name = "guid")
   @AccessType("property")
   public long getId() {
      return id;
   }

   /**
    * @return the guid
    */
   @Id
   @OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, optional = false, targetEntity = Guid.class)
   @PrimaryKeyJoinColumn(name = "guid")
   @JoinColumn(name = "guid", insertable = true)
   public Guid getGuid() {
      return guid;
   }



}



GUID:

Code:

public class Guid {
        private long guid;

/* Other stuff cut */
   
   /**
    * @return the guid
    */
   @Id
   @Column(name = "guid")
   @GeneratedValue(strategy = GenerationType.AUTO)
   public long getGuid() {
      return guid;
   }
}


The Failure message is:

Code:
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`xxxxx`.`person`, CONSTRAINT `FK232148F4EDAE0DB` FOREIGN KEY (`guid`) REFERENCES `guid` (`guid`))
   at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1666)
   at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1082)
   at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
   at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)


Any help much appreciated, the requirements metioned above are non-negotiable and do need to be achieved; I know they are awkward and unnatural. Also, long string based UUIDs are out. We don't necessarily have to have a separate GUID object but we must be able to retrieve ANY GUID in ANY table, just by the id. This is something that could be done by Hibernate Search which we're also looking into, but the solution described above is seen as the ideal if we can get it working.

Sorry for all the words!

Chris.


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