欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

myeclipse通过数据表生成jpa或hibernate实体

程序员文章站 2022-05-04 16:23:32
...

就是说,如果你已经有了数据库表,那么你又不想手工书写jpa或hibernate实体(要谁都不想,还要自己写那么一大堆的注解),myeclipse能够帮助你自动生成。

1、首先你需要创建一个jpa项目:

如图在普通项目上点右键添加

myeclipse通过数据表生成jpa或hibernate实体

2、打开生成工作环境

myeclipse通过数据表生成jpa或hibernate实体

3、创建数据库连接

因为,myeclipse也要连接数据库获取表结构

myeclipse通过数据表生成jpa或hibernate实体

4、填写连接信息

myeclipse通过数据表生成jpa或hibernate实体

5、打开表列表,选择导出模式

myeclipse通过数据表生成jpa或hibernate实体


6、保存导出实体工程选择

(这一步需要第1步的操作,否则,没有可选工程)

myeclipse通过数据表生成jpa或hibernate实体

现在就生成了entity,如:

package com.partner4java.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * User entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "user", catalog = "hello_world")
public class User implements java.io.Serializable {

	// Fields

	private Integer id;
	private String username;
	private String password;
	private String address;

	// Constructors

	/** default constructor */
	public User() {
	}

	/** minimal constructor */
	public User(String username, String password) {
		this.username = username;
		this.password = password;
	}

	/** full constructor */
	public User(String username, String password, String address) {
		this.username = username;
		this.password = password;
		this.address = address;
	}

	// Property accessors
	@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 = "username", nullable = false, length = 20)
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "password", nullable = false, length = 20)
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "address", length = 20)
	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}