日期:2014-05-16 浏览次数:20791 次
把 Myeclipse 转到DB Browser
?
新建一个Database Connection driver
?
然后找到要反向工程的表
?
右键->Hibernate Reverse Engineering
然后,选好entity缩放的目录
对 Create POJO<>DB Table mapping information 打钩
然后点选 AddHibernate mapping annotations to POJO
再点选Update Hibernate configuration with mapping resource location
?
其他钩全部去掉,点击下一步,直至结束就可以
?
这样生成的entity
?
这里只说一对多和多对一
?
例:用户和组为多对一关系,双向
?
用户类:
package com.hibernate.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
 * Tuser entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "tuser", catalog = "test")
public class Tuser implements java.io.Serializable {
	// Fields
	/**
	 * 
	 */
	private static final long serialVersionUID = -7792597282750540598L;
	private Integer id;
	private Tgroup tgroup;
	private String name;
	// Constructors
	/** default constructor */
	public Tuser() {
	}
	/** full constructor */
	public Tuser(Tgroup tgroup, String name) {
		this.tgroup = tgroup;
		this.name = name;
	}
	// Property accessors
	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "groupid")
	public Tgroup getTgroup() {
		return this.tgroup;
	}
	public void setTgroup(Tgroup tgroup) {
		this.tgroup = tgroup;
	}
	@Column(name = "name")
	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
?
组类:
package com.hibernate.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
 * Tgroup entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "tgroup", catalog = "test")
public class Tgroup implements java.io.Serializable {
	// Fields
	/**
	 * 
	 */
	private static final long serialVersionUID = -7208715716759269846L;
	private Integer id;
	private String name;
	private Set<Tuser> tusers = new HashSet<Tuser>(0);
	// Constructors
	/** default constructor */
	public Tgroup() {
	}
	/** full constructor */
	public Tgroup(String name, Set<Tuser> tusers) {
		this.name = name;
		this.tusers = tusers;
	}
	// Property accessors
	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Column(name = "name")
	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "tgroup")
	public Set<Tuser> getTusers() {
		return this.tusers;
	}
	public void setTusers(Set<Tuser> tusers) {
		this.tusers = tusers;
	}
}
?
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
	<session-factory>
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<pr