[Hibernate] - Annotations
Hibernate使用Annotations最简单例子:
hibernate.cfg.xml
<?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> <!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1/testdb</property>
<property name="connection.username">root</property>
<property name="connection.password"></property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</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.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property> <mapping class="com.my.bean.User"/> </session-factory> </hibernate-configuration>
HibernateUtil.java
package com.my.dao.util; import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
return configuration.configure().buildSessionFactory(
new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build());
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
} public static SessionFactory getSessionFactory() {
return sessionFactory;
} }
POJO
package com.my.bean; import java.util.Date; import javax.persistence.*; @Entity
@Table(name="user")
public class User {
@Id @GeneratedValue @Column(name="user_id", nullable=false)
private long userID; @Column(name="user_name", length=100, nullable=false)
private String userName; @Column(name="create_time", nullable=false)
private Date createTime; public long getUserID() {
return userID;
} public void setUserID(long userID) {
this.userID = userID;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
Test:
package com.my.init;
import java.util.Date; import org.hibernate.Session;
import org.hibernate.Transaction; import com.my.bean.User;
import com.my.dao.util.HibernateUtil; public class Test { public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction(); try {
User user = new User();
user.setUserName("Robin");
user.setCreateTime(new Date()); session.save(user); tx.commit();
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
} session.close();
} }
参考文献:
http://www.tutorialspoint.com/hibernate/hibernate_annotations.htm
[Hibernate] - Annotations的更多相关文章
- [Hibernate] - Annotations - Many To Many
Hibernate annotation 多对多: 下面测试例子会自动生成一张表:card,这张是bank和user表的映射表.里头是bank_id和user_id两个组合字段. 如果想在这张映射表中 ...
- [Hibernate] - Annotations - One To One
Hibernate annotation 一对一的两种实现: 1)幅表中有主表的主键ID做为引用 2)幅表的主键即为主表的ID hibernate.cfg.xml <?xml version=& ...
- JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-007UserTypes的用法(@org.hibernate.annotations.Type、@org.hibernate.annotations.TypeDefs、CompositeUserType、DynamicParameterizedType、、、)
一.结构 二.Hibernate支持的UserTypes接口 UserType —You can transform values by interacting with the plain JD ...
- JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-005控制类型映射(Nationalized、@LOB、@org.hibernate.annotations.Type)
一.简介 1. 2. 3. 4. to override this default mapping. The JPA specification has a convenient shortcut a ...
- java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataProvider
Caused by: java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/MetadataProvi ...
- BAE 环境下 hibernate annotations 配置
annotations 配置 首先需要加入 hibernate-jpa-2.0-api-1.0.1.Final.jar 和 ejb3-persistence.jar 这两个包 ejb3-persis ...
- Caused by: java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataPro
1.错误描述 信息: MLog clients using java 1.4+ standard logging. 2014-7-12 19:29:20 com.mchange.v2.c3p0.C3P ...
- Hibernate Annotations 注解
Hibernate Annotations 注解 对于org.hibernate.annotations与org.hibernate.persistence,它的注释比如Columns,可是不知道怎么 ...
- [Hibernate] - Annotations - One To Many
Hibernate使用Annotation的一对多: hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8&q ...
随机推荐
- zendstudio添加注释快捷键
最常用的是ctrl+/来添加行注释 方法是选中代码,按ctrl+/就可以注释掉代码.再按ctrl+/可以去掉注释 添加块注释时,现加/,再加*,再按enter即可添加块注释.
- markdown简明语法
# markdown简明语法 标签(空格分隔): markdown 本语法只涵盖了常用的内容 [toc] 标题 标题 标题 语法为: 根据需求 可以指定 不同大小的标题 # 顶级 ## 次级 ### ...
- Codeforces Round #297 (Div. 2)
A题 题目大意: 给你一个字符串,奇数的时候是钥匙,偶数的时候是门,一把钥匙只能开对应的门,然后问你最少额外需要多少把钥匙. 分析: 用的数组记录一下就行,(注意的是先开门,再拿钥匙!开始错在这里了, ...
- mysql-server 的一些记录
为避免 innodb 文件的增大.个人倾向于读立表空间.以 innodb_file_per_table=1 参数调整. 不使用默认数据目录的话,须得将 grp own 都循环设置为mysql. 昨天晚 ...
- twisted的defer模式和线程池
前言: 最近帮朋友review其模块服务代码, 使用的是python的twisted网络框架. 鉴于之前并没有使用过, 于是决定好好研究一番. twisted的reactor模型很好的处理了网络IO事 ...
- 用java将excel表格中的内容修改后写入到另一个excel中
package nn; import java.io.File; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl. ...
- Codeforces Round #367 (Div. 2) A B C 暴力 二分 dp(字符串的反转)
A. Beru-taxi time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 16年大连网络赛 1006 Football Games
题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=725&pid=1006 Football Games Time ...
- ZOJ 1029 Moving Tables
原题链接 题目大意:走廊两边排列了400个房间,要在两个房间之间搬桌子.搬桌子的时候会占用一部分走廊,有冲突的话要回避.求最快搬完的时间. 解法:开辟一个数组,每占用一段走廊,就把相应的房间号的元素加 ...
- tyvj1013 - 找啊找啊找GF ——二维背包变种
题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1013 好吧,这题没节操=_= 状态f[u,v,i]表示:消费u的人民币和v的人品同时泡到i个mm所需要的 ...