-->
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.  [ 13 posts ] 
Author Message
 Post subject: defining composite primary key using xdoclet
PostPosted: Mon Nov 24, 2003 10:26 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
Is it possible to simply define primary key composed from more than 1 field using xdoclet and if, how. Any example please.

I can't find good examples of using hibernate+xdoclet.

Thanks

Johnny


Top
 Profile  
 
 Post subject: Possible but ugly solution
PostPosted: Mon Nov 24, 2003 3:39 pm 
Beginner
Beginner

Joined: Fri Oct 10, 2003 4:54 pm
Posts: 26
Location: Chicago, IL
Here is an approach that works, but it's not exactly the nicest solution. The biggest problem is that the id class is required to implement Serializable as well as the equals and hashCode methods.

I suppose it would be possible to enhance XDoclet to generate the code for the internal id class. At any rate, here ya go...


public class BankAccount {
private Id _id;
private String _name;

public static class Id implements Serializable {
private String _bankAccountNumber;
private String _bankAbaNumber;
public Id() {
}
/**
* @hibernate.property column="BANK_ACCT_NUM"
*/
public String getBankAccountNumber() { ... }
/**
* @hibernate.property column="BANK_ABA_NUM"
*/
public String getBankAbaNumber() { ... }
//.... setters, and must implement equals and hashCode
}

public BankAccount() {
}
/**
* @hibernate.id class="BankAccount$Id" generator-class="assigned"
*/
public Id getId() { return _id; }
/**
* @hibernate.property column="NAME"
*/
public String getName() { return _name}

//....
}


Top
 Profile  
 
 Post subject: composite prim. key using xdoclet without special id class?
PostPosted: Tue Nov 25, 2003 4:03 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
I would like to define primary key compounded from more than one filed using xdoclet simply without using special id class.
This should result to generation of xml like this:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="com.exit.test.hibernate.beans.AssetClass"
table="ASSET_CLASS_TBL"
dynamic-update="false"
dynamic-insert="false"
>

<composite-id>
<key-property name="assetClassID"/>
<key-property name="name"/>
</composite-id>

<property
name="assetClassID"
column="assetClassID"
type="java.lang.Integer"
/>

<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="name"
/>

<property
name="description"
type="java.lang.String"
update="true"
insert="true"
column="description"
length="200"
/>

......etc........


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 7:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
If your using XDoclet have you not decided on a topdown development approach thus you will implement the ID class anyway. So adding the tags there for the mapping document is correct is it not? I dont think I understand your development method. You want to generate (partially) the POJOs?


Top
 Profile  
 
 Post subject: id class
PostPosted: Tue Nov 25, 2003 10:31 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
Ok, I am new to hibernate. So there is no way to have POJO object, that have composit primary key without special id-class?

I am generating POJO objects with XDoclet tags from old system from java files with some attributes in comments (similar to POJO with XDoclet, but different, proprietary solution).

So I must generate id-classes where composite primary key is used?
I think it is not very good solution. Because primary key is not component. It is only subset of columns in entity, table, POJO...
I would like simply mark with XDoclet tag, that this column is part of primary key, this column is also part of primary key, and this is simply property of entity, POJO.....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 8:33 pm 
Beginner
Beginner

Joined: Fri Oct 10, 2003 4:54 pm
Posts: 26
Location: Chicago, IL
I'm not positive, but I think Hibernate requires you to have "PK" classes for compound primary keys (at least that is always the way I have used it when I can't get away from the evils of compound keys).

One thing that might help you though to expand on my example is that you can provide "helper" methods on the domain objects like so:

Code:
public class BankAccount {
    private Id _id;
    private String _name;

    public static class Id implements Serializable {
        private String _bankAccountNumber;
        private String _bankAbaNumber;
        /**
        * @hibernate.property column="BANK_ACCT_NUM"
        */
        public String getBankAccountNumber() { ... }
        /**
        * @hibernate.property column="BANK_ABA_NUM"
        */
        public String getBankAbaNumber() { ... }
        //.... setters, and must implement equals and hashCode
    }

    /**
     * @hibernate.id class="BankAccount$Id" generator-class="assigned"
     */
    public Id getId() { return _id; }
    /**
     * @hibernate.property column="NAME"
     */
    public String getName() { return _name}

    public String getBankAccountNumber() {
        return _id == null ? null : _id.getBankAccountNumber();
    }

    public String getBankAbaNumber() {
        return _id == null ? null : _id.getBankAbaNumber();
    }

//....
}


At least this approach makes the internal id classes transparent to most application code. You can also provide helpers on the setter methods if you want.

It's a bit of a pain, but then again so are a lot of things when dealing with tables with compound primary keys.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 8:43 pm 
Beginner
Beginner

Joined: Tue Nov 25, 2003 3:33 pm
Posts: 35
[quote="loverde"]I'm not positive, but I think Hibernate requires you to have "PK" classes for compound primary keys (at least that is always the way I have used it when I can't get away from the evils of compound keys).[/quote]


This is not correct. Hibernate does not require that. It is totaly legal to say something like:


<composite-id>
<key-property name="n1" column="col1" />
<key-property name="n2" column="col2" />
<key-property name="n3" column="col3" />
</composite-id>

as long as your persistent class is Serializable and has equals and hasCode ()
methods this will work.

The problem is with XDoclet it just has no capabilities to generate the above (at least the latest one from sf.net) by say looking at @hibernate.key-property or something like that.

Alex.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 11:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
It is true that you can have external PK class (recommended approach) or use an integrated version. As far as I know XDoclet does not currently support integrated compound Keys.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 4:36 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
So what is conclusion?

1. I must use id-class if I want to use XDoclet+compositePrimaryKeys?

2. Or there is possible to use some merging to achieve:
<composite-id>
<key-property name="n1" column="col1" />
<key-property name="n2" column="col2" />
</composite-id>


Top
 Profile  
 
 Post subject: vote for XDoclet open improvement
PostPosted: Wed Nov 26, 2003 6:13 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
Hi boys, there is open improvement in XDoclet forum, this is exactly answer to my question:

http://opensource.atlassian.com/projects/xdoclet/secure/ViewIssue.jspa?key=XDT-414&watch=true


Please vote for this task to go things move forward. Before vote you must login (signup).

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 9:04 am 
Beginner
Beginner

Joined: Tue Nov 25, 2003 3:33 pm
Posts: 35
I can only tell you what I did.

Open up the xdt that generates your mapping and insert an extra insertion point for the kind of composite-id that you need. Then you will only need those composite ids described in a file that will be merged.

Inserting the merge point is not brainer just grep for the phrase that is being generated currently in your mapping file, that says you need to create some sort of file to be able to provide additional info for this mapping. Note, that the problem with this insertion point is that it comes a bit too late, to insert composite-id so you will need your own.

It is a bit ugly, but works fine for me.

HTH,
Alex.


Top
 Profile  
 
 Post subject: can you help me more with merging
PostPosted: Wed Nov 26, 2003 9:46 am 
Regular
Regular

Joined: Fri Nov 21, 2003 10:23 am
Posts: 81
Can you help me more how to do it with merging, any example, link?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 9:55 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The solution should be sumitted to XDoclet JIRA for consideration for inclusion. That way the community will benefit.


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