用myeclipse的Hibernate 反向引擎 生成 数据库的 entity

把 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>
		<property name="connection.url">
			jdbc:mysql://localhost/test
		</property>
		<property name="connection.username">root</property>
		<property name="connection.password">root</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>

		<!-- JDBC connection pool (use the built-in) -->
		<property name="connection.pool_size">1</property>



		<!-- Enable Hibernate's automatic session context management -->
		<property name="current_session_context_class">thread</property>

		<!-- Disable the second-level cache  -->
		<property name="cache.provider_class">
			org.hibernate.cache.NoCacheProvider
		</property>

		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">true</property>

		<property name="format_sql">true</property>
		<!--
			<mapping class="com.hibernate.entity.Husband" />
			<mapping class="com.hibernate.entity.Wife" />
			===================================================
			<mapping class="com.hibernate.entity.Teacher" />
			<mapping class="com.hibernate.entity.Student" />
		-->

		<mapping class="com.hibernate.entity.Tgroup" />
		<mapping class="com.hibernate.entity.Tuser" />
	</session-factory>

</hibernate-configuration>
 


这里会抛错,原因是user类中没有写cascade = CascadeType.ALL

在Tuser类的@ManyToOne(fetch = FetchType.LAZY)中加上cascade = CascadeType.ALL就可以了

@Test
	public void saveUser() {
		Tgroup g = new Tgroup();
		g.setName("g1");
		
		Tuser u = new Tuser();
		u.setName("u1");
		u.setTgroup(g);
		
		Session s = sessionFactory.getCurrentSession();
		s.beginTransaction();
		s.save(u);
		s.getTransaction().commit();//这里会抛错,原因是user类中没有写cascade = CascadeType.ALL
,而默认是不写的。	}

 

 

这个方法也可以持久化两个类

@Test
	public void saveGroup() {
		Tgroup g = new Tgroup();
		g.setName("g1");
		
		Tuser u = new Tuser();
		u.setName("u1");
		u.setTgroup(g);
		
		g.getTusers().add(u);
		
		Session s = sessionFactory.getCurrentSession();
		s.beginTransaction();
		s.save(g);
		
		s.getTransaction().commit();
	}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值