2014-09-20 156 views
0

我使用JBoss Studio/Eclipse中的Hibernate Tools的最新版本從我的數據庫模式生成Java POJO。Hibernate工具 - POJO命令

當有一個獲取更多對象的get方法時,我希望按特定順序返回這些對象。很明顯,我可以編輯類文件,但如果這些文件重新生成,如果有任何進一步的模式更改,這些更改將會丟失。研究表明應該可以使用hibernate反向工程XML配置或可能的hibernate hbm xml文件來添加這些選項。 我似乎無法讓這些工作,並希望有人可以提供一個可以工作的示例配置。

我已經創建了一個非常簡單的數據庫來說明問題。

SQL架構:

-- 
-- 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 */; 

這將導致兩個POJO的。

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。 getPupils方法需要在創建POJO時添加一個OrderBy註解。這可能需要是List而不是Set?

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; 
    } 

} 

感謝

+0

@OrderBy註釋就足夠了,即使它是一個List或Set。你得到了什麼輸出? http://www.javacodegeeks.com/2012/04/hibernate-tip-sort-and-order.html – user23123412 2014-09-20 15:23:16

+0

@ user23123412,如何在POJO生成時添加此註釋? 我在網上找到的幾個例子似乎沒有任何效果,並且在eclipse中似乎沒有輸出來幫助調試問題出現的位置 – hedgehog14 2014-09-20 17:10:46

回答

0

例通過逆向工程XML添加註釋: 你可以看到它是多麼容易調整範圍類METE attibute在下面的例子。

<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> 

您可以在字段級別使用範圍字段屬性。 這個完整的元屬性link