我使用JBoss Studio / Eclipse的最新版本的Hibernate Tools从我的数据库架构生成Java 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 :(得分: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>
您可以使用scope-field属性在字段级别上。 此link
的完整元属性