Hibernate,JPA注解@Entity
2024-08-28 23:37:47
通过@Entity注解将一个类声明为一个实体bean(即一个持久化POJO类), @Id注解则声明了该实体bean的标识属性. 其他的映射定义是隐式的.
就是说一个持久化POJO类,除了主键ID需要@Id显示注解,其他列都可以不做任何注解。
用例代码如下:
- 数据库DDL语句:
create table CAT
(
id VARCHAR2(32 CHAR) not null,
create_time TIMESTAMP(6),
update_time TIMESTAMP(6),
cat_name VARCHAR2(255 CHAR)
)
- hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库驱动配置 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
<property name="connection.username">wxuatuser</property>
<property name="connection.password">xlh</property>
<property name="show_sql">true</property>
<!-- 自动执行DDL属性是update,不是true -->
<property name="hbm2ddl.auto">update</property>
<!-- hibernate实体类 --> <mapping class="a1_Entity.Cat"/> </session-factory>
</hibernate-configuration>
- java类
实体类 - 基类
package model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import org.hibernate.annotations.GenericGenerator; /**
* 实体类 - 基类
*/
@MappedSuperclass
public class BaseEntity implements Serializable { private static final long serialVersionUID = -6718838800112233445L; private String id;// ID
private Date create_time;// 创建日期
private Date update_time;// 修改日期
@Id
@Column(length = 32, nullable = true)
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Column(updatable = false)
public Date getCreate_time() {
return create_time;
}
public void setCreate_time(Date create_time) {
this.create_time = create_time;
}
public Date getUpdate_time() {
return update_time;
}
public void setUpdate_time(Date update_time) {
this.update_time = update_time;
}
@Override
public int hashCode() {
return id == null ? System.identityHashCode(this) : id.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass().getPackage() != obj.getClass().getPackage()) {
return false;
}
final BaseEntity other = (BaseEntity) obj;
if (id == null) {
if (other.getId() != null) {
return false;
}
} else if (!id.equals(other.getId())) {
return false;
}
return true;
}
}
实体类
package a1_Entity;
import javax.persistence.Entity; import model.BaseEntity; @Entity
public class Cat extends BaseEntity{
/**
* 实体类
*/
private static final long serialVersionUID = -2776330321385582872L; private String cat_name; public String getCat_name() {
return cat_name;
} public void setCat_name(String cat_name) {
this.cat_name = cat_name;
}
}
Dao
package daoUtil; import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder; public class HibernateUtil { private static final SessionFactory sessionFactory; static {
try {
Configuration cfg = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).buildServiceRegistry();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
// Log exception!
throw new ExceptionInInitializerError(ex);
}
} public static Session getSession() throws HibernateException {
return sessionFactory.openSession();
} public static Object save(Object obj){
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(obj);
tx.commit();
} catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
return obj;
} public static void delete(Class<?> clazz,String id){
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Object obj = session.get(clazz,id);
session.delete(obj);
tx.commit();
} catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
}
main
package a1_Entity; import daoUtil.HibernateUtil; public class Test_Entity { public static void main(String[] args) {
Cat cat = new Cat();
cat.setId("1");
cat.setCat_name("test1@Entity");
HibernateUtil.save(cat);
}
}
环境:JDK1.6,MAVEN
源码地址:http://files.cnblogs.com/files/xiluhua/hibernate%40Entity.rar
最新文章
- c# 局域网文件传输实例
- [No000053]我25岁了,是应该继续挣钱,还是选择自己的爱好?--正好庆祝自己25岁生日
- Flume 实战(2)--Flume-ng-sdk源码分析
- Python: 解决pip安装源被墙的问题
- [BS-14] 打印NSArray和NSDictionary的3种方法
- 给Activity设置背景颜色
- .NET单例模式-------各种写法&;&;验证
- StrPCopy与StrPas功能正好相反,作用是与C语言字符串和Delphi的String相互转化
- hdu4770:Lights Against Dudely(回溯 + 修剪)
- Error with mysqld_safe
- Part 4:表单和类视图--Django从入门到精通系列教程
- Batch_Size 详解
- ElasticSearch(五) Elasticsearch-jdbc实现MySQL同步到ElasticSearch
- 离线部署 pm2
- Vue中常用的三种传值方式
- Linux中/etc/resolv.conf文件简析
- java copy 文件夹
- [Objective-C语言教程]指针(15)
- Laravel是怎么实现autoload的?
- Bandit:一种简单而强大的在线学习算法
热门文章
- Power-BI For K3 免费版培训与交流!Q群视频培训,绝对干货!
- Android界面实现----PagerTabStrip绚丽的滑动标签
- 一台电脑多个文件夹安装多个Redis服务
- Linux 动态链接库学习笔记
- python gzip,bz2学习
- iOS6:在你的App内使用Passbook
- layoutsubviews什么时候调用
- configuring tortoise git and vs code.
- ";数学口袋精灵";bug的发现及单元测试
- (转)Aspone.Cells设置Cell数据格式 Setting Display Formats of Numbers and Dates