-->
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.  [ 2 posts ] 
Author Message
 Post subject: hbm2java Initializing attribute in generated POJO
PostPosted: Thu Jul 30, 2009 1:06 am 
Newbie

Joined: Wed Jul 29, 2009 11:50 pm
Posts: 1
I'm using Hibernate Tools 3.2.4.GA to reverse engineer from a MySQL 5.1 database via Ant.

I would like to initialize the employee attribute in the generated POJO:
Code:
@Entity
@Table(name="user"
    ,catalog="testdb"
)
public class User  implements java.io.Serializable {

     private Employee employee;
     private String userName;
...


To something like:
Code:
private Employee = new Employee();


I have added the classes folder to the tools classpath but it still doesn't work.

User.hbm.xml:
Code:
...
<hibernate-mapping>
    <class name="org.app.model.User" table="user" catalog="testdb">
        <id name="id" type="string">
            <column name="emp_id" length="20" />
            <generator class="foreign">
                <param name="property">employee</param>
            </generator>
        </id>
        <one-to-one name="employee" class="org.app.model.Employee" constrained="true"></one-to-one>
        <property name="userName" type="string">
            <column name="usr_userName" length="45" not-null="true" />
        </property>
...


Top
 Profile  
 
 Post subject: Re: hbm2java Initializing attribute in generated POJO
PostPosted: Sun Aug 02, 2009 10:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
chent wrote:
I'm using Hibernate Tools 3.2.4.GA to reverse engineer from a MySQL 5.1 database via Ant.

I would like to initialize the employee attribute in the generated POJO:
Code:
@Entity
@Table(name="user"
    ,catalog="testdb"
)
public class User  implements java.io.Serializable {

     private Employee employee;
     private String userName;
...


To something like:
Code:
private Employee = new Employee();


I have added the classes folder to the tools classpath but it still doesn't work.


Why should that matter ?

Anyway here is how to do it:
Quote:
User.hbm.xml:
Code:
...
<hibernate-mapping>
    <class name="org.app.model.User" table="user" catalog="testdb">
        <id name="id" type="string">
            <column name="emp_id" length="20" />
            <generator class="foreign">
                <param name="property">employee</param>
            </generator>
        </id>
        <one-to-one name="employee" class="org.app.model.Employee" constrained="true">
          <meta attribute="default-value">new Employee()</meta>
       </one-to-one>
        <property name="userName" type="string">
            <column name="usr_userName" length="45" not-null="true" />
        </property>
...


Notice the meta attribue line.

_________________
Max
Don't forget to rate


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