Myeclipse 10 using hibernate create annotation (Annotation) entity class

To MySQL database as an example, please build the corresponding table in a database.

1. Configure database link

Open Myelipse Database Explorer view

Window-->Open Perspective-->MyEclipse Database Explorer

New Connection in the right-Browser column DB: New ...

Fill out the information: Remember to import the package, otherwise it will error

Driver name: the name of which is displayed in MyEclipse;

Connection URL: URL link to the database, here to mysql , for example;

User name: database user name;

Password: password database;

Click Add IARs, select the drive package,

Following the addition jar package, pay attention to see Driver classname driver name is correct,

Click Test Driver, pop-up window:

Enter the database password, the following pop-up window, database links normal;

Click finish, in MyEclipse's DB Browser window you can see the database you just added

Double-click hebradio_record, find the corresponding database table:

2. Generate hibernate profile and class SersionFactory

Myeclipse adjust the view, in the upper right corner of MyEclipse, choose MyEclipse  the Java  Enterprise view:

Create a new web project, and set up relevant package structure:

Select the Right Project -> MyEclipse -> add Hibernate Capabilities 

如果选择Hibernate4.1,Enable Hibernate Annotations Support 会直接被勾选上,如果Hibernate3.3,这一项默认没有勾选,这一项勾选上就是以注解的方式生成实体类

点击next

点击next,如图:

在DB Driver选择我们上面配置好的数据库信息,点击next

选择存放HibernateSessionFactory类的包,这里我就放到model下面,点击Finish,生成hibernate配置文件及SeeionFactory类,如图:

3.生成实体类

MyEclipse右上角切换到MyEclipse Database Explorer视图:

到DB Brower中找到要反向的表选中并且点击右键--->Hibernate Reverse Enginnering

Java src folder 源码包,不用多解释,java package——存放将要反转出来的实体类,选择目录(就是之前建好的com.xxx.model之类的包);

Create pojo<>db。。。这个选项选中,就是我们建立从表到简单java 对象(即pojo)的配置,把下面的add hibernate mapping annotations to pojo的选上,其它不管,这个选项用来“添加映射注解到pojo对象上”,它上面的那个选项用来创建xml的;

Java Data Object(POJO <> DB Table),去掉Create abstract class 这项,否则会为每个实体类生成一个抽象的类

点击 next,如图:

type Mapping要选上hibernatetypes,这样这些注解都是来自 javax.persistence.*了;

id generator 看下拉列表就知道是配置id生成策略的;

那两个enable 是说映射关系发现(detection)

点击next,如图:

Class name: the name of the entity corresponding to the class table;

id generator: ID generation strategy;

Click Finish, can generate the corresponding entity class.

Guess you like

Origin www.cnblogs.com/Jeely/p/11225665.html