How to generate Hibernate mapping files & annotation with Hibernate Tools

转自:https://www.mkyong.com/hibernate/how-to-generate-code-with-hibernate-tools/

In this article, we show you how to use Hibernate / JBoss Tools to generate Hibernate mapping files (hbm) and annotation code from database automatically.

Tools in this article

  1. Eclipse v3.6 (Helios)
  2. JBoss / Hibernate Tools v3.2
  3. Oracle 11g
  4. JDK 1.6

1. Hibernate Perspective

Open your “Hibernate Perspective“. In Eclipse IDE, select “Windows” >> “Open Perspective” >> “Others…” , choose “Hibernate“.

2. New Hibernate Configuration

In Hibernate Perspective, right click and select “Add Configuration…

In “Edit Configuration” dialog box,

  1. In “Project” box, click on the “Browse..” button to select your project.
  2. In “Database Connection” box, click “New..” button to create your database settings.
  3. In “Configuration File” box, click “Setup” button to create a new or use existing “Hibernate configuration file”,hibernate.cfg.xml.

See your list of your tables in “Hibernate Perspective“.


Sample of “hibernate.cfg.xml“, connect to Oracle 11g database.

<?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">
<hibernate-configuration>
<session-factory>
  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:MKYONG</property>
  <property name="hibernate.connection.username">mkyong</property>
  <property name="hibernate.connection.password">password</property>
  <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  <property name="hibernate.default_schema">MKYONG</property>
 </session-factory>
</hibernate-configuration>

3. Hibernate Code Generation

Now, you are ready to generate the Hibernate mapping files and annotation codes.

– In “Hibernate Perspective”, click “Hibernate code generation” icon (see below figure) and select “Hibernate Code Generation Configuration”


– Create a new configuration, select your “console configuration” (configured in step 2), puts your “Output directory” and checked option “Reverse engineer from JDBC Connection“.


– In “Exporter” tab, select what you want to generate, Model , mapping file (hbm) , DAO, annotation code and etc.


See result


Note
The generated Hibernate mapping file and annotations code are very clean, standard and easy to modify. Try explore more features.

展开阅读全文

hibernate annotation 继承 外键

04-19

xx.javarn[code=Java]package bean;rnrnimport java.io.Serializable;rnrnimport javax.persistence.Column;rnimport javax.persistence.Entity;rnimport javax.persistence.Id;rnimport javax.persistence.Table;rnrn@Entityrn@Table(name="XX")rnpublic class xx implements Serializable rn private int id;rn private String name;rnrn @Idrn public int getId() rn return id;rn rnrn public void setId(int id) rn this.id = id;rn rnrn @Columnrn public String getName() rn return name;rn rnrn public void setName(String name) rn this.name = name;rn rnrn[/code]rnyy.javarn[code=Java]package bean;rnrnimport java.io.Serializable;rnrnimport javax.persistence.Id;rnimport javax.persistence.JoinColumn;rnimport javax.persistence.ManyToOne;rnimport javax.persistence.MappedSuperclass;rnrn@MappedSuperclassrnpublic class yy implements Serializable rn private int id;rn private xx x;rnrn @Idrn public int getId() rn return id;rn rnrn public void setId(int id) rn this.id = id;rn rnrn @ManyToOnern @JoinColumn(name="xid")rn public xx getX() rn return x;rn rnrn public void setX(xx x) rn this.x = x;rn rnrnrn[/code]rnzz.javarn[code=Java]package bean;rnrnimport java.io.Serializable;rnrnimport javax.persistence.Column;rnimport javax.persistence.Entity;rnimport javax.persistence.Table;rnrn@Entityrn@Table(name="ZZ")rnpublic class zz extends yy implements Serializable rn private String comment;rnrn @Columnrn public String getComment() rn return comment;rn rnrn public void setComment(String comment) rn this.comment = comment;rn rnrn[/code]rn这样为什么就生产了 一张表 XX?rn 论坛

Hibernate Annotation 的问题!!

10-23

我在项目已加载hibernate3.2jar和hibernate_annotation.jar的包和已用jdk1.6,但我在entity类输入(写)@id,@Column时总是报错找不到符号:rnpackage hibernateinfo;rnrnimport org.hibernate.annotations.AccessType;rnimport org.hibernate.annotations.Entity;rnimport org.hibernate.annotations.Table;rnimport org.hibernate.mapping.Column;rn/**rn *rn * @author jackyrn */rn@Entity//这里不显示错误rn@AccessType("property")rnrn[color=#FF0000]@Table(name="per")//这里显示错误[/color]rnrnpublic class per implements java.io.Serializablern rn private Integer id;rn private String name;rn private String address ;rn private String tel;rn private String passward ;rn rn public per() rn rn rn [color=#FF0000]@id//这里显示错误[/color] rn [color=#FF0000][/color]@Column(name="id")//这里显示错误rn public void setId(Integer _id)rn rn id = _id ;rn rn rn public Integer getId()rn rn return id ;rn rn @Column(name="name")// 显示错误rn public void setName(String _name)rn rn name = _name;rn rn rn public String getName()rn rn return name ;rn rn @Column(name="passward")//显示错误rn public void setPassward(String _passward)rn rn passward =_passward ;rn rn rn public String getPassward()rn rn return passward ;rn rn rn @Column(name = "address")//显示错误rn public void setAddress(String _address)rn rn address =_address;rn rn rn public String getAddress()rn rn return address ;rn rn @Column(name = "tel")//显示错误rn public void setTel(String _tel)rn rn tel =_tel;rn rn rn public String getTel()rn rn return tel ;rn rnrn请各位帮帮手,谢谢!!!!!!!!1 论坛

没有更多推荐了,返回首页