[Hibernate] - Annotations
2014-08-25 17:01
225 查看
Hibernate使用Annotations最简单例子:
hibernate.cfg.xml
HibernateUtil.java
POJO
Test:
参考文献:
http://www.tutorialspoint.com/hibernate/hibernate_annotations.htm
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 3 Annotations 进阶,不错,发扬一下
- org.hibernate.annotations.Entity和javax.persistence.Entity 区别
- Hibernate 3 Annotations 进阶
- hibernate annotations
- Spring 3 Annotations - HibernateDaoSupport - Repository Requires Session Factory
- log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version). log4j:WA
- hibernate-annotations总结
- hibernate annotations
- hibernate annotations @Entity hql "XX not mapped"
- Hibernate Annotations
- Hibernate中自动增长列的 在Annotations 中的配置和数据库设置默认值问题
- Caused by: java.lang.ClassNotFoundException: org.hibernate.annotations.Entity
- Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0 Annotations注解框架整合及其代码分享
- Caused by: java.lang.NoSuchMethodError: org.hibernate.cfg.annotations.reflection.XMLContext$Default.
- [转]Hibernate 3 Annotations 进阶
- Hibernate 3 Annotations进阶教程
- hibernate annotations @Entity hql "XX not mapped"
- org.hibernate.annotations.Entity和javax.persistence.Entity区别、使用
- JavaPersistenceWithHibernate第二版笔记-第七章-005排序的集合(@org.hibernate.annotations.SortComparator)
- Hibernate-annotations 方式设置字段的默认值