aspose file tools*
The moose likes Object Relational Mapping and the fly likes hibernate-mapping file and hbm2java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate-mapping file and hbm2java" Watch "hibernate-mapping file and hbm2java" New topic
Author

hibernate-mapping file and hbm2java

Jami Kapla
Greenhorn

Joined: Aug 31, 2004
Posts: 2
Is there a way to make hbm2java generator used the unsaved-value as the default in the POJO you create?

I would like to use hbm2java to create my POJOs and never have to edit them (as I prefer middle out to top down). HOWEVER I need the POJOs to have the id default to unsaved-value otherwise I have to set that manually (and the best way to do that would be to edit the generated POJO).

What I would like is for the following hbm :
---------------------------------------------------
<hibernate-mapping>
<class name="com.investing.pd.Investor" table="investors"
dynamic-update="false" dynamic-insert="false">

<id name="investorId" column="investor_id" type="int"
unsaved-value="-1">
<generator class="native"></generator>
</id>


To create something like:
-----------------------------------------------
public class Investor implements Serializable {

/** identifier field */
private int investorId = -1;

IS THIS POSSIBLE?
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Why not trying to use as unsaved-value the Java default value for the corresponding type?

Otherwise another solution would be to have a meta with class-code where you initialize the field with the needed value:



blog - InfoQ.com
Jami Kapla
Greenhorn

Joined: Aug 31, 2004
Posts: 2
Thanx for the help.

Unfortunately the <meta attribute="class-code"
produced no discernable difference from hbm2java.

As far as the first suggestion I guess I don't understand what it is you mean, as it is an int not sure what the java default is for that.
But nonetheless the whole reason I need to do this is to get it to work with a pre-existing framework that expects unsaved POJO's to have a identifier of -1(int)

-JK
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by Jami Kapla:
Unfortunately the <meta attribute="class-code"
produced no discernable difference from hbm2java.


It is supposed to place the code included in the <meta> element into the generated class. I think it must work.

As far as the first suggestion I guess I don't understand what it is you mean, as it is an int not sure what the java default is for that.


Java int default value is 0. So if you set this value for the unsaved-value, there will be no more need for tricks.

But nonetheless the whole reason I need to do this is to get it to work with a pre-existing framework that expects unsaved POJO's to have a identifier of -1(int)


If none of the above does not work you can always go to AOP (AspectJ, Aspectwerkz) and solve it (even if in this case I don't recommend it being in this case a hack :-( )

/pope
Max Andersen
Greenhorn

Joined: Sep 13, 2004
Posts: 2
The meta attribute "class-code" must work if you are using the latest version.

/max


Originally posted by Jami Kapla:
Is there a way to make hbm2java generator used the unsaved-value as the default in the POJO you create?

I would like to use hbm2java to create my POJOs and never have to edit them (as I prefer middle out to top down). HOWEVER I need the POJOs to have the id default to unsaved-value otherwise I have to set that manually (and the best way to do that would be to edit the generated POJO).

What I would like is for the following hbm :
---------------------------------------------------
<hibernate-mapping>
<class name="com.investing.pd.Investor" table="investors"
dynamic-update="false" dynamic-insert="false">

<id name="investorId" column="investor_id" type="int"
unsaved-value="-1">
<generator class="native"></generator>
</id>


To create something like:
-----------------------------------------------
public class Investor implements Serializable {

/** identifier field */
private int investorId = -1;

IS THIS POSSIBLE?


/max
 
wood burning stoves
 
subject: hibernate-mapping file and hbm2java