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

来源:互联网 发布:手机跑分软件 编辑:程序博客网 时间:2024/06/14 11:10

如果你已经有了数据库表,那么你又不想手工书写jpa或hibernate实体,myeclipse能够帮助你自动生成。


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

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


2、打开生成工作环境


3、创建数据库连接

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


4、填写连接信息


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



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

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


现在就生成了entity,如:

[java] view plain copy
  1. package com.partner4java.entity;  
  2.   
  3. import javax.persistence.Column;  
  4. import javax.persistence.Entity;  
  5. import javax.persistence.GeneratedValue;  
  6. import static javax.persistence.GenerationType.IDENTITY;  
  7. import javax.persistence.Id;  
  8. import javax.persistence.Table;  
  9.   
  10. /** 
  11.  * User entity. @author MyEclipse Persistence Tools 
  12.  */  
  13. @Entity  
  14. @Table(name = "user", catalog = "hello_world")  
  15. public class User implements java.io.Serializable {  
  16.   
  17.     // Fields  
  18.   
  19.     private Integer id;  
  20.     private String username;  
  21.     private String password;  
  22.     private String address;  
  23.   
  24.     // Constructors  
  25.   
  26.     /** default constructor */  
  27.     public User() {  
  28.     }  
  29.   
  30.     /** minimal constructor */  
  31.     public User(String username, String password) {  
  32.         this.username = username;  
  33.         this.password = password;  
  34.     }  
  35.   
  36.     /** full constructor */  
  37.     public User(String username, String password, String address) {  
  38.         this.username = username;  
  39.         this.password = password;  
  40.         this.address = address;  
  41.     }  
  42.   
  43.     // Property accessors  
  44.     @Id  
  45.     @GeneratedValue(strategy = IDENTITY)  
  46.     @Column(name = "id", unique = true, nullable = false)  
  47.     public Integer getId() {  
  48.         return this.id;  
  49.     }  
  50.   
  51.     public void setId(Integer id) {  
  52.         this.id = id;  
  53.     }  
  54.   
  55.     @Column(name = "username", nullable = false, length = 20)  
  56.     public String getUsername() {  
  57.         return this.username;  
  58.     }  
  59.   
  60.     public void setUsername(String username) {  
  61.         this.username = username;  
  62.     }  
  63.   
  64.     @Column(name = "password", nullable = false, length = 20)  
  65.     public String getPassword() {  
  66.         return this.password;  
  67.     }  
  68.   
  69.     public void setPassword(String password) {  
  70.         this.password = password;  
  71.     }  
  72.   
  73.     @Column(name = "address", length = 20)  
  74.     public String getAddress() {  
  75.         return this.address;  
  76.     }  
  77.   
  78.     public void setAddress(String address) {  
  79.         this.address = address;  
  80.     }  
  81.   
  82. }  

0 0
原创粉丝点击