• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate-mapping file and hbm2java

 
Jami Kapla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Jami Kapla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic