【问题标题】:Hibernate tools - POJO orderingHibernate 工具 - POJO 排序
【发布时间】:2014-11-14 23:58:35
【问题描述】:

我正在使用来自 JBoss Studio / Eclipse 的最新版本的 Hibernate Tools 从我的数据库架构中生成 Java POJO。

当有一个 get 方法可以获取更多对象时,我希望这些对象以特定顺序返回。显然我可以只编辑类文件,但是如果有任何进一步的架构更改,如果重新生成这些文件,这些更改将会丢失。

研究表明应该可以使用休眠逆向工程 XML 配置或休眠 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。

瞳孔.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;
    }

}

学校.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;
    }

}

谢谢

【问题讨论】:

  • @OrderBy 注释就足够了,即使它是 List 或 Set。你得到了什么输出? javacodegeeks.com/2012/04/hibernate-tip-sort-and-order.html
  • @user23123412,如何在 POJO 生成时添加此注释?我在网上找到的几个例子似乎没有任何效果,而且eclipse中似乎没有输出来帮助调试问题可能出在哪里

标签: java eclipse hibernate hibernate-tools


【解决方案1】:

通过逆向工程xml添加注释的示例: 您可以在下面的示例中看到调整范围类mete 属性是多么容易。

<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

处的完整元属性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多