forked from Hackergeek/JavaWebBase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestDatabase.java
More file actions
62 lines (55 loc) · 1.89 KB
/
Copy pathTestDatabase.java
File metadata and controls
62 lines (55 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package entity;
import java.util.EnumSet;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.junit.Test;
public class TestDatabase {
//
// // 添加测试数据
// @Test
// public void testAddStudent() {
// // 创建配置对象
// Configuration configuration = new Configuration().configure();
// // 创建SessionFactory对象
// SessionFactory sessionFactory = configuration.buildSessionFactory();
// // 创建session对象
// Session session = sessionFactory.getCurrentSession();
// // 创建事务对象
// Transaction ts = session.beginTransaction();
//
// Student s1 = new Student("S0000001", "skyward", "男", new Date(), "未知行星");
// Student s2 = new Student("S0000002", "geek", "男", new Date(), "未知行星");
// Student s3 = new Student("S0000004", "hacker", "男", new Date(), "未知行星");
//
// session.save(s1);
// session.save(s2);
// session.save(s3);
//
// ts.commit();
// sessionFactory.close();
// }
@Test
public void testSchemaExport() {
// 创建配置对象
Configuration configuration = new Configuration().configure();
// 创建服务注册对象
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.configure().build();
// 创建SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 创建session对象
Session session = sessionFactory.getCurrentSession();
// 创建SchemaExport对象
SchemaExport schemaExport = new SchemaExport();
Metadata metadata = new MetadataSources(serviceRegistry)
.buildMetadata();
schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);
}
}