Hibernate 第五讲 数据库Id生成策略 (1)
来源:互联网 发布:新视野大学英语软件 编辑:程序博客网 时间:2024/05/21 10:22
hibernate.cfg.xml
<?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> <!-- Database connection settings --> <!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> --> <!-- SQL dialect --> <!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property> --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:hibernate</property> <property name="connection.username">scott</property> <property name="connection.password">tiger</property> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping class="com.darren.hibernate.model.Student" /> <mapping class="com.darren.hibernate.model.Teacher" /> </session-factory></hibernate-configuration>实体类配置方式:
package com.darren.hibernate.model;import java.util.Date;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Teacher { private int id; private String name; private String title; private String yourWifeName; private Date birthDate; @Id @GeneratedValue // id 生成侧略,对于mysql来说,生成策略为auto_increment,对于oracle来说,生成策略为hibernate_serence public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getYourWifeName() { return yourWifeName; } public void setYourWifeName(String yourWifeName) { this.yourWifeName = yourWifeName; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; }}测试类:
package com.darren.hiobernate.model;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;import com.darren.hibernate.model.Student;import com.darren.hibernate.model.Teacher;public class HibernateIDTest { private static SessionFactory sessionFactory; @BeforeClass public static void beforeClass() { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } @AfterClass public static void afterClass() { sessionFactory.close(); } @Test public void testStudentSave() { Student s = new Student(); s.setAge(8); Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); session.save(s); session.getTransaction().commit(); } @Test public void testTeacherSave() { Teacher t = new Teacher(); t.setId(1); t.setName("t1"); t.setTitle("middle"); t.setBirthDate(new Date()); Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); session.save(t); session.getTransaction().commit(); } public static void main(String[] args) { beforeClass(); }}
- Hibernate 第五讲 数据库Id生成策略 (1)
- Hibernate 第五讲 数据库Id生成策略 (2)
- Hibernate 第五讲 数据库Id生成策略 (3)
- Hibernate 第五讲 数据库Id生成策略 (4)
- Hibernate 第五讲 数据库Id生成策略 (5)
- Hibernate框架学习(第三讲)---Hibernate注解以及ID生成策略
- Hibernate ID 生成策略
- hibernate id 生成策略
- hibernate ID生成策略
- Hibernate ID 生成策略
- Hibernate id生成策略
- hibernate---ID生成策略
- hibernate id生成策略
- hibernate id 生成策略
- hibernate--ID生成策略
- Hibernate学习(四)ID生成策略
- hibernate ID生成策略(sequence)
- hibernate ID生成策略(xml)
- How to install and use GCC g++ v4.7 and C++11 on Ubuntu 12.04
- java bingfa
- Java Code之线程(二)
- Good book about java
- MAPI
- Hibernate 第五讲 数据库Id生成策略 (1)
- DOS命令批量修改文件后缀名
- 对ACM的态度
- PHPUnit require_once()报错问题
- Windows Azure中文培训材料下载
- Machine Learning---LMS 算法
- 180E - Cubes DP 优先队列
- Android--DatePickerDialog日期控件
- BIT 1063 食物链