0

I'm using the latest version of Hibernate Tools from JBoss Studio / Eclipse to generate Java POJOs from my database schema.

When there is a get method that gets further objects I want to have these returned in a specific order. Obviously I can just edit the class file however these changes will then be lost if these files are re-generated if there are any further schema changes.

Research has showed it should be possible to add these options using hibernate reverse engineering XML config or possibly a hibernate hbm xml file. I can't seem to get these to work and am hoping somebody could provide an example config that would work.

I have created a very simple DB to illustrate the problem.

SQL Schema:

--
-- Table structure for table `pupil`
--

DROP TABLE IF EXISTS `pupil`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `pupil` (
  `ID` int(11) NOT NULL,
  `school_id` int(11) NOT NULL,
  `forename` varchar(100) NOT NULL,
  `surname` varchar(100) NOT NULL,
  `gender` enum('M','F') NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `fk_pupils_schools_idx` (`school_id`),
  CONSTRAINT `fk_pupils_schools` FOREIGN KEY (`school_id`) REFERENCES `school` (`ID`)     ON DELETE NO ACTION ON UPDATE NO ACTION)
  ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `school`
--

    DROP TABLE IF EXISTS `school`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `school` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

This results in two POJOs.

Pupil.java

package hibernateExample.db;  
// Generated 20-Sep-2014 15:34:31 by Hibernate Tools 4.0.0

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * Pupil generated by hbm2java
 */
@Entity
@Table(name = "pupil", catalog = "test")
public class Pupil implements java.io.Serializable {

    private int id;
    private School school;
    private String forename;
    private String surname;
    private String gender;

    public Pupil() {
    }

    public Pupil(int id, School school, String forename, String surname,
            String gender) {
        this.id = id;
        this.school = school;
        this.forename = forename;
        this.surname = surname;
        this.gender = gender;
    }

    @Id
    @Column(name = "ID", unique = true, nullable = false)
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "school_id", nullable = false)
    public School getSchool() {
        return this.school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    @Column(name = "forename", nullable = false, length = 100)
    public String getForename() {
        return this.forename;
    }

    public void setForename(String forename) {
        this.forename = forename;
    }

    @Column(name = "surname", nullable = false, length = 100)
    public String getSurname() {
        return this.surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    @Column(name = "gender", nullable = false, length = 2)
    public String getGender() {
        return this.gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

}

School.java. The getPupils method needs to have an OrderBy annotation added on POJO creation. This probably needs to be a List rather than a Set too?

package hibernateExample.db;
// Generated 20-Sep-2014 15:34:31 by Hibernate Tools 4.0.0

import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * School generated by hbm2java
 */
@Entity
@Table(name = "school", catalog = "test")
public class School implements java.io.Serializable {

    private Integer id;
    private String name;
    private Set<Pupil> pupils = new HashSet<Pupil>(0);

    public School() {
    }

    public School(String name, Set<Pupil> pupils) {
        this.name = name;
        this.pupils = pupils;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "name", length = 200)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "school")
    public Set<Pupil> getPupils() {
        return this.pupils;
    }

    public void setPupils(Set<Pupil> pupils) {
        this.pupils = pupils;
    }

}

Thanks

  • @OrderBy annotation will be enough, even if it is a List or Set. What output did you get? http://www.javacodegeeks.com/2012/04/hibernate-tip-sort-and-order.html – Ken de Guzman Sep 20 '14 at 15:23
  • @user23123412, how do I get this annotation added at the point of POJO generation? A couple of examples I've found online don't seem to have any effect and there seems to be no output in eclipse to help debug where the problem might lie – hedgehog14 Sep 20 '14 at 17:10

1 Answers1

0

Example to add the an annotation via reverse engineering xml: You can see how easy it is to tweak the scope class mete attibute in below example.

<table name="organization">
        <meta attribute="scope-class">@Proxy(lazy=false) public</meta>
        <meta attribute="extra-import">org.hibernate.annotations.Proxy</meta>
        ....rest of config.....
</table>

You could on a field level using scope-field attribute. Full meta attributes at this link

maggu
  • 1,201
  • 9
  • 9