Reverse Engineering with MAVEN Hibernate5

Source: Internet
Author: User

In the actual development, the table structure is generally designed first, and then implement the code, so it is more common to generate database entity analogy based on table structure. Say what you do, and act.

Create a table


CREATE TABLE [dbo].[Classes](
[class_id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[create_time] [datetime] NULL,
CONSTRAINT [PK_Classes] PRIMARY KEY CLUSTERED
(
[class_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON

Create a console Configuration

New-> Select Console Configuration->next:

Check under Hibernate version, project, database, configuration file. After the point is determined:

Some people may not have this view, open the Window->show view->other->hibernate->hibernate configuration to see that view.

To create a reverse engineering configuration file

This file can also be created when the generated code is started. But I'm just going to create it.

Next

Next

Select Console Configuration,refresh, and select the table to generate the code for.

Now that the Hibernate.reveng.xml file has been created, but we need to do some configuration, the default may not be appropriate.

Open the Reveng.xml file and switch to:

If it goes well, clicking "Add" will pop up the database library and let you choose. If you have more than one console configuration, you are prompted to select one first.

But I did not smooth ah, the choice of the console configuration after any reaction is not. So I started all kinds of tossing:

折腾1:修改console configuration的hibernate版本为5.0以下。居然可以生成代码了,激动,可是代码报错。折腾2:升级eclipse版本,这个是我灵光一闪觉得可能是我的hibernate版本太高了,hibernate tools不支持,可是hibernate tools可是跟着eclipse走的。下载中。。。。下载了Mar2版本不可以安装Jboss tools 4.4.0,下载了Neon版本,天啊噜,看到Jboss tools 4.4.0了,心里又是一阵激动。可惜安装不成功,反复了几次,姐不干了,准备第二天再试(ps:之前就碰到过当天安装不成功的,改天就行了,怀疑网络问题)。第二天一早我就开始重新安装Jboss tools,good,成功了,成功了,不仅安装成功,也可以添加Tables,可以见证我的成功:

I'm starting to get excited and ready to generate code.

Don't see this button, it doesn't matter, it doesn't matter, open from window->perspetive, such as:

Back to the point, start configuring the configuration that runs the generated code:

Run up, the code comes out:

Configure Cfg.xml

The code is generated, and the mapping relationship with the database also has, but Hibernate does not know where the mapping file Ah, it is necessary to tell a sound, modify the Cg.xml, add the following configuration, of course, with tools to add more simple:

<mapping resource="Classes.hbm.xml"/>

I put Classes.hbm.xml down the resources file, so with Cfg.xml is a directory, the actual situation where the file is configured to the appropriate path.

Modify Loginaction


public String execute() throws Exception {
Session ss = getSession();
ss.beginTransaction();
/*ss.save(PersonShana);
ss.getTransaction().commit();*/
Classes classes = new Classes();
classes.setName("语文");
classes.setCreateTime(new Date());
ss.save(classes);
ss.getTransaction().commit();
ss.close();
return "success";
}
static Session getSession() {
if (sessionFactory == null) {
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
}
return sessionFactory.openSession();
}

Run

Open Http://localhost:8080/webtest/login

The database has data.

Demo

Download

Reverse Engineering with MAVEN Hibernate5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.