用myeclipse的Hibernate 反向引擎 生成 数据库的 entity
2017-10-17 18:00
471 查看
把 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
这里只说一对多和多对一
例:用户和组为多对一关系,双向
用户类:
组类:
这里会抛错,原因是user类中没有写cascade = CascadeType.ALL
在Tuser类的@ManyToOne(fetch = FetchType.LAZY)中加上cascade = CascadeType.ALL就可以了
这个方法也可以持久化两个类
新建一个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(); }
相关文章推荐
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- J2EE进阶(十五)MyEclipse反向工程实现从数据库反向生成实体类之Hibernate方式[申明:来源于网络]
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- hibernate反转引擎使用(通过MyEclipse的反向生成工具)
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- 图文教程:使用MyEclipse的hibernate工具从数据库反向生成实体Bean
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- 【原创】MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- 利用MyEclipse从数据库反向生成实体类之Hibernate方式
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- 使用MyEclipse中用Hibernate反向工程生成的DAO会发生对象无法存储到数据库的现象,原因是没有运用事务。
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- MyEclipse从数据库表反向生成实体类之Hibernate方式(反向工程)
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
- MyEclipse从数据库反向生成实体类之Hibernate方式