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>




原创粉丝点击