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

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

时间:2023-02-19 11:02:35浏览次数:31  
标签:Hibernate javax name myeclipse entity Tgroup import public persistence


把 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();
}

 


标签:Hibernate,javax,name,myeclipse,entity,Tgroup,import,public,persistence
From: https://blog.51cto.com/u_21817/6066625

相关文章

  • Hibernate 性能优化_1
    大概如此:不一定说在每个项目中都合适 1、比如,开了N多文件而没关,比如开了地址池而没清,比如分页读了N多页而没有清内存 2、对于ManyToOne,如果设为FetchType=Eager,则会产生1+......
  • Hibernate 的 join
    转的: 1.如果没有在Hibernate配置里做关联,在Hql里面是不能用join的。2.Hql里面的join是没有"ON"子句的。3.如果是innerjoin,Join的依据就是事先设计好关联4.如果要用......
  • Hibernate 性能优化_3
    二级缓存 对于二级缓存,其实并不一定要在项目中使用除非是对项目要求非常高的情况下使用 如果要用,应使用在:经常被访问,改动不大,数量不多,比如权限,比如组织机构 load()默认使......
  • Hibernate 性能优化_2
    createQuery("FROM****").list()和createQuery("FROM****").iterate()的区别 1、list()时,会取出所有的数据,Iterate()时,只取所有记录的主键,当用到哪条时,再根据id去取哪条......
  • .net core 微服务之 Ocelot 集成 IdentityServer4
    为什么集成在Ocelot中在上一篇文章中,我们并没有将认证授权集成在Ocelot中,在生产环境中,如果我们每个服务都添加了认证授权,那么在实际部署中肯定会生成很多的配置,就会相对于......
  • keycloak 添加 identity provider
    1.  启动keycloak:下载keycloak-20.0.3.zip,windows版本。2.kc.batstart-dev,本地可以打开 localhost:80803.启动一个openIdconnect服务。这里用ident......
  • 如何修复identity 2的默认登录路由中的错误
    将services.ConfigureApplicationCookie移到services.AddIdentity之后,最重要的是删除服务中的AddDefaultUI。在此引用以下是更改默认登录页面的代码:service......
  • 【Azure 事件中心】通过 az rest --method get 如何获得Event Hub Entity 级的统计指
    问题描述通过azrest--methodget 如何获得EventHubEntity级的统计指标? 问题解答查阅文档  ​​https://learn.microsoft.com/en-us/rest/api/monitor/metrics/li......
  • 【Azure 事件中心】通过 az rest --method get 如何获得Event Hub Entity 级的统计指
    问题描述通过azrest--methodget 如何获得EventHubEntity级的统计指标? 问题解答查阅文档  https://learn.microsoft.com/en-us/rest/api/monitor/metrics/l......
  • NestJs使用之EntityMetadataNotFoundError: No metadata for "Article" was found.
    前言博主使用TypeRom配置数据库后,在请求数据库的findAll()方法时,报错,我的报错截图如下:我的实体也使用了@Entity进行了注册。而且引入方式正确。仍然报这个错误,我花费了......