简体   繁体   中英

Generating Hibernate POJO classes

I am generating hibernate pojo classes with annotations from existing tables using Ant Script . I am stuck with a problem. Problem is that I am having two classes Person and Address. There is OneToMany mapping between these two classes.

Generated classes contains :

//Person.java
@OneToMany(fetch=FetchType.LAZY)
public Set<Address> getAddresses()
{
    return addresses;
}
public void setAddresses(Set<Address> addresses)
{
    this.addresses=addresses;
}

//Address.java
@OneToMany(fetch=FetchType.LAZY)
public Person getPerson()
{
     return person;
}
public void setPerson(Person person)
{
     this.person=person;
}

I am having a situation where I don't want to generate the set of addresses. Is there any way to generate the POJOs without set. ie to avoid the reverse mapping. Please help. Thanks in advance.

I'm making an assumption here, but I think you use the Hibernate Tools for Ant to generate entity classes. These tools (as far as I remember) generates classes based on Hibernate mapping XMLs (hbm.xml).

I am generating hibernate pojo classes with annotations from existing tables using Ant Script. I am stuck with a problem. Problem is that I am having two classes Person and Address. There is OneToMany mapping between these two classes .

You've said that the Person and Address class, which are, or at least should be, mappings of tables in your database, are in a 1:N relation. (One person can have multiple addresses, while an address can belong only to a single person.) As such, you should have a Person.hbm.xml and Address.hbm.xml file somewhere around. This means, that if your mapping files are correct then a Set<Address> field (with a getter and setter method) will be generated by the Hibernate Tools, because that's how it works.

I'm not an active user of this tool, but as I see you have the following options.

  1. Change your tables (and mappings) so that Person and Address would be in a 1:1 relation (not so cool).
  2. If you only want to restrict your clients from using the getAddresses and setAddresses methods, then you can modify the generated methods visibility. See the Hibernate Tools Reference Guide's section on Controlling POJO Code Generation (especially the subsection on meta attributes).

(I really don't know much about Hibernate Tools, so maybe it's possible to entirely omit the methods in question via some configuration, but it would be really inappropriate to do so nonetheless.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM