-->
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.  [ 10 posts ] 
Author Message
 Post subject: XDoclet, composite id's and ant merge dirs
PostPosted: Thu Sep 09, 2004 8:52 am 
Newbie

Joined: Wed Sep 01, 2004 5:31 am
Posts: 18
Hibernate version:2.0

Hi,

Im having some trouble using xdoclet with hibernate and composite identifiers. I managed to figure out that xdoclet has no support for composite id's with hibernate and one way of 'getting' the id into the hbm.xml file without doing it by hand is to use the merge dir in the ant script. The merge dir would include the xml tags for the composite id into the hbm.xml file. My problem is that the xml file with the extra tags are never included.

My merge dir is specified by eg. mergeDir="C:/xml/merge" for a class: myClass in package: myPackage
And i've put the xml to be merged in the suitable catalogue under this directory (given by the package structure) eg: C:/xml/merge/myPackage/hibernate-properties-myClass.xml

Does anyone see why this should not work? I would be very greatful if anyone have any input in this matter what so ever.

Regards / A. Kikr


Top
 Profile  
 
 Post subject: Update
PostPosted: Thu Sep 09, 2004 9:06 am 
Newbie

Joined: Wed Sep 01, 2004 5:31 am
Posts: 18
Ok, it seems that the xml is merged! But the problem still persists. I added an empty id tag to my java file and now the generation goes through and i can see that the merge has been made but the generation won't go through without the id tag (which is what i want). So it seems the generated xml is checked before additional information is merged into it. Can this really be the case? Any comments anyone?


Top
 Profile  
 
 Post subject: workaround
PostPosted: Thu Sep 09, 2004 11:49 am 
Newbie

Joined: Wed Sep 01, 2004 5:31 am
Posts: 18
Does anyone have a workaround for XDoclets lack of support of composite keys for hibernate? I cant think up anything good. Any comments helpful.

Cheers / A. Kikr


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 09, 2004 12:23 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Well, considering XDoclet DOES support composite keys I'm not quite sure where you're breaking down.

For our comp keys, we have comp key objects. The class that has the comp key maps the hibernate.id to the comp key object. Then the key object simply has hibernate.property or hibernate.many-to-one tags and it sorts it all out.

Now, if you're having problem with the key many-to-one to an object with a composite id then xdoclet didn't (still doesn't?) handle that. Although my co-worker submitted a patch (to Xdoclet) to provide support for it that we use.

In the end, typically if Xdoclet doesn't do what you need it to do it is sometimes just as easy to modify the xtd(?) files to make it do what you need.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 2:29 am 
Newbie

Joined: Wed Sep 01, 2004 5:31 am
Posts: 18
Thanks for the reply Vampboy,

Yes, you are quite right. It seems xdoclet does support composite keys. The thing was i found no information on how to do it or even that it worked, only som post about how it wasn't supported. But indeed i got it to work and it was really the logical way to do it to. I guess I just wasn't thinking straight yesterday :)
If anyone else is struggeling with the same thing here's what to do:

Say you have three classes: Client, Custom and Field. Client has a one-to-many realtionship to Custom, Field also has a one-to-many relationship to Custom and Custom stores a value about the association (M-M) between Client and Field. Cusom should have a composite key of the id of Client and the id of Field. The composite key elements should go in a separate class calles say CustomKey with Custom having a CustomKey instance called say id.
To Customadd:

/**
* @hibernate.id generator-class="CustomKey"
*/

To CustomKey add:

/**
* @hibernate.many-to-one column="field_id" class="myPackage.Field"
*/

/**
* @hibernate.many-to-one column="client_id" class="myPackage.Client"
*/

This worked for me, there's probably a prettier solution or a better one. Thanks for the help though. This forum is awsome!

Cheers / A.kikr


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 2:56 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Yep, that is how you do it. Although you don't have to actually specify the class. Xdoclet and Hibernate will figure that all out.

i.e.

Code:
/**
  * @hibernate.id
  */
public AgreementConditionsPK getPrimaryKey() {


Out of curiousity are the key many to ones getting generated properly? Actually I think the actual issue is key many to ones where the one side is made up of a comp key so I think you should be fine for now :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 2:56 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Yep, that is how you do it. Although you don't have to actually specify the class. Xdoclet and Hibernate will figure that all out.

i.e.

Code:
/**
  * @hibernate.id
  */
public AgreementConditionsPK getPrimaryKey() {


Out of curiousity are the key many to ones getting generated properly? Actually I think the actual issue is key many to ones where the one side is made up of a comp key so I think you should be fine for now :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 20, 2004 9:30 am 
Beginner
Beginner

Joined: Wed Sep 15, 2004 3:14 am
Posts: 35
I am having the same problem as azzkikr had. Do I need to generate the Setters and Getters in the CustomKey class? Would it be possible for you to provide me a complete example? Sorry I am new to this.

I have this code but it doesn't work:

Code:
/**
* @hibernate.class table="Custom"
*/
public class Custom ... {
   private CustomKey id = new CustomKey();
   private myPackage.Field field;
   private myPackage.Client client;

    public Custom(Long field_id, Long client_id) {
      this.field_id = field_id;
      this.client_id = client_id;

      this.id.field_id = field.getId();
      this.id.client_id = client.getId();

      field.getCustom.add(this);
      client.getCustom.add(this);
    }

   /**
     * @hibernate.id generator-class="CustomKey" unsaved-value="any"
     */
   public CustomKey getId () { return id; }
}

public class CustomKey ... {
   protected Long field_id;
   protected Long client_id;

   public CustomKey() {}

   /**
     * @hibernate.many-to-one column="roleID"
     * @hibernate.many-to-one column="userID"
     */
   public CustomKey(Long field_id, Long client_id) {
      this.field_id = field_id;
      this.client_id = client_id;
   }
}


Please help, what should I do? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 21, 2004 3:25 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Newreaders,

I did get your email but I'll respond here instead.

As far as I know this shouldn't end up generating anything....

Code:
/**
     * @hibernate.many-to-one column="roleID"
     * @hibernate.many-to-one column="userID"
     */
   public CustomKey(Long field_id, Long client_id) {
      this.field_id = field_id;
      this.client_id = client_id;
   }


What you should have is a getter for each of your properties.

In your case if you are simply using Longs to store your properties then it would simply be:

@hibernate.property="roleID"
public Long getRoleID();

@hibernate.property="userID"
public Long getUserID();

Other than that I'm not quite sure what you're trying to do in the Custom class.

Your email stated that you wanted to set up a many-to-many association. If that's just the case and your many doesn't contain any extra info (i.e. it is just a junction table) then you don't actually even need to declare the class.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 21, 2004 6:41 pm 
Beginner
Beginner

Joined: Wed Sep 15, 2004 3:14 am
Posts: 35
Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.