Maven+Hibernate+JPA+Postgresql基础配置

1.persistence.xml必须放在META-INF目录下

目录结构如下图所示:


2.pom.xml配置代码如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.examples</groupId>
  <artifactId>hibernate</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>hibernate Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
  	<postgresql.version>9.3-1103-jdbc3</postgresql.version>
  	<junit-version>4.10</junit-version>
  	<hibernate-version>5.0.0.Final</hibernate-version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit-version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate-entitymanager</artifactId>
    	<version>${hibernate-version}</version>
    </dependency>
    <dependency>
		<groupId>org.postgresql</groupId>
		<artifactId>postgresql</artifactId>
		<version>${postgresql.version}</version>
	</dependency>
  </dependencies>
  <build>
    <finalName>hibernate</finalName>
  </build>
</project>
3.persistence.xml配置代码如下

<persistence
        version="2.1"
        xmlns="http://xmlns.jcp.org/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
                            http://xmlns.jcp.org/xml/ns/persistence_2_1.xsd">
  <persistence-unit name="jpa-2" transaction-type="RESOURCE_LOCAL">  
        <!-- 配置使用什么ORM产品作为JPA的实现  
            1. 实际上配置的是接口javax.persistence.spi.PersistenceProvider的实现类  
            2. 若项目只有一个实现产品时,可以不用配置  
         -->  
        <provider>org.hibernate.ejb.HibernatePersistence</provider>  
          
        <properties>  
            <!-- 连接数据库的基本信息 -->  
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres"/>  
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>  
            <property name="javax.persistence.jdbc.user" value="postgres"/>  
            <property name="javax.persistence.jdbc.password" value="123456"/>  
              
            <!-- 配置JPA实现产品的基本属性。因为使用hibernate,故也就是配置hibernate的基本属性 -->  
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>  
            <property name="hibernate.show_sql" value="true"/>  
            <property name="hibernate.format_sql" value="true"/>  
            <!--   
                hibernate.hbm2ddl.auto参数的作用主要用于:自动创建|更新|验证数据库表结构。如果不是此方面的需求建议set value="none"。   
                create:   
                每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。   
                create-drop :   
                每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。 update:   
                最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等   
                应用第一次运行起来后才会。  
                validate :   
                 每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。  
             -->  
            <property name="hibernate.hbm2ddl.auto" value="update"/>  
        </properties>  
    </persistence-unit>  
</persistence> 
4.实体配置如下

package org.jpwh.model.helloworld;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
public class Message {

 
    @Id
    @GeneratedValue
    private Long id;

    private String text;

    public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}
@Entity注释指名这是一个实体Bean,@Table注释指定了Entity所要映射带数据库表,其中@Table(name="t_message")用来指定映射表的表名。如果缺省@Table注释,系统默认采用类名作为映射表的表名。实体Bean的每个实例代表数据表中的一行数据,行中的一列对应实例中的一个属性。

5.测试类代码如下

package org.jpwh.helloworld;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.jpwh.model.helloworld.Message;
import org.junit.BeforeClass;
import org.junit.Test;

public class JpaTest {
	@BeforeClass
	public static void setUpBeforClass() throws Exception{
		
	}
	
	@Test
	public void createTable(){
		//可以验证生成表是否正确 
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa-2");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		Message mssg = new Message();
		mssg.setText("test");
		em.persist(mssg);
		em.getTransaction().commit();
		em.close();
		emf.close();
	}
}

其中createEntityManagerFactory("jpa-2")的jpa-2要和persistence.xml中的<persistence-unit name="jpa-2" name对应,否则会报错找不到对应的name



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值