HelloHibernate 的创建过程

ZhuYuanxiang 2019-01-25 00:00:00
Categories: Tags:

安装与配置

开发小结

建立项目

1
2
3
CREATE TABLE [dbo].[MESSAGE] (
[MESSAGE] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]

配置项目

创建代码

1
2
3
4
5
6
7
8
9
10
package sample.entity;
public class Message {
private String message;
public Message(String message) {
this.message = message;}
public String getMessage() {
return message;}
public void setMessage(String message) {
this.message = message;}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package sample.entity;
public class PopulateMessages {

public static void main(String[] args) {
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();

Message message = new Message("Hibernated");
session.save(message);
session.getTransaction().commit();
session.close();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="Hibernate">
<property name="hibernate.connection.driver_class">
net.sourceforge.jtds.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=hibernate
</property>
<property name="hibernate.connection.username">
sa
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="hibernate.show_sql">
true
</property>
<mapping resource="sample/entity/Message.hbm.xml"/>
</session-factory>
</hibernate-configuration>

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2019-1-23 19:49:53 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="sample.entity.Message" table="MESSAGE">
<id name="message" type="java.lang.String">
<column name="MESSAGE"/>
<generator class="assigned"/>
</id>
</class>
</hibernate-mapping>

执行项目