JPA持久化注释和Hibernate
来源:互联网 发布:手机mac地址可以修改吗 编辑:程序博客网 时间:2024/06/10 19:15
花了某天的所有空闲时间,终于把jpa持久化操作和hibernate相结合,
直接去掉了hibernate实体类的*.hbm.xml文件,大大减少了代码的繁琐和映射文件,
多个实体类之间能进行更方便的操作。
实体类:Person.java
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity (name="person1")
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String Pname;
public Person() {
//对象是由Hibernate为我们创建的,当我们通过ID来获取某个实体的时候,这个实体给我们返回了
//这个对象的创建是由Hibernate内部通过反射技术来创建的,反射的时候用到了默认的构造函数,
//所以这时候必须给它提供一个public的无参构造函数
}
public Person(String name) {
this.Pname = name;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO) //auto是默认值,可不写
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "pname", length = 50)
public String getPname() {
return Pname;
}
public void setPname(String pname) {
Pname = pname;
}
}
操作类:HibernateUtil.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sf;
private HibernateUtil(){}
static {
Configuration cfg=new Configuration().configure();
sf=cfg.buildSessionFactory();
}
public static Session getSession(){
Session session=sf.getCurrentSession();
return session;
}
}
接口及实现类:
PersonDaoImpl.java
import com.sise.entity.Person;
public interface PersonDaoImpl {
public void addPerson(Person person);
public Person getPersonById(Integer personid);
}
PersonDao.java:
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.sise.entity.Person;
public class PersonDao implements PersonDaoImpl {
@Override
public void addPerson(Person person) {
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
try {
session.persist(person);
tx.commit();
} catch (Exception e) {
if(null!=tx)tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
@Override
public Person getPersonById(Integer personid) {
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
Person person=(Person)session.get(Person.class, personid);
tx.commit();
session.close();
return person;
}
}
测试类;
PersonTest.java:
import com.sise.bean.PersonDao;
import com.sise.bean.PersonDaoImpl;
import com.sise.entity.Person;
public class PersonTest {
private static PersonDaoImpl personDao=new PersonDao();
public static void main(String[] args) {
Person person = new Person();
person.setPname("测试");
personDao.addPerson(person);
Person p1=personDao.getPersonById(1);
System.out.println("id:"+p1.getId()+",名字"+person.getPname());
}
}
hibernate配置文件:
hibernate.cfg.xml
<!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>
<!-- DB连接四要素 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/lunhua</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--C3P0 数据源(数据库连接池)
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
-->
<!-- 当前Session上下文 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 显示SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL -->
<property name="hibernate.format_sql">true</property>
<!-- 注册映射文件 -->
<mapping class="com.sise.entity.Person" />
</session-factory>
</hibernate-configuration>
包
- JPA持久化注释和Hibernate
- jodatime持久化hibernate jpa
- 持久化框架ibatis、hibernate和Jpa优缺点分析
- Spring整合Hibernate实现JPA持久化
- Java for Web学习笔记(九七):持久化初探(2)ORM、JPA和Hibernate
- EntityManager持久化实体(Hibernate面向jpa编程)
- JPA+Hibernate持久化数据:detached entity passed to persi
- 【面试之持久化框架】hibernate、mybatis、jpa规范
- JPA持久化异常:PersistenceException: org.hibernate.PersistentObjectException
- JPA注解持久化
- 07.JPA持久化
- JPA 和hibernate-jpa-api
- JPA Guice的JPA持久化
- JPA数据持久化技术
- 数据持久化框架(JPA)
- JPA注解持久化类
- JPA和Hibernate注解
- hibernate 和jpa
- 继承中的初始化顺序
- 653. Two Sum IV
- 史上最全!阿里智能人机交互的核心技术解析
- 索引的设计和使用
- ffmpeg安装
- JPA持久化注释和Hibernate
- java中的数据类型
- MySQLSTR_TO_DATE函数
- springboot学习总结系列(一):多环境下(开发环境,生产环境)使用yml 或 properties 配置文件
- nginx基本配置与参数说明
- C# 文件上传 默认最大为4M的解决方法
- 将代码块从一台电脑快速挪到另一台电脑上
- Map函数的学习与理解
- property