没有为EntityManager指定缓存的持久性提供程序

时间:2013-06-03 04:52:17

标签: java xml hibernate ehcache pom.xml

Message.java

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

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name="MESSAGES")
@Cache(region = "messages", usage = CacheConcurrencyStrategy.READ_WRITE)

public class Message {

    Message(){

    }
    Message(String message){
        message_text=message;
    }
    @Id @GeneratedValue
    @Column(name="MESSAGE_ID")
    public Long id;
    @Column(name="MESSAGE_TEXT")
    public String message_text;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getMessage_text() {
        return message_text;
    }
    public void setMessage_text(String message_text) {
        this.message_text = message_text;
    }
}

ehcache.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">    

    <defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />        
    <cache name="messages" maxElementsInMemory="10" eternal="true" overflowToDisk="false" />  

 </ehcache>

的persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    <persistence-unit name="annotation">
        <properties>
            <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@ebiz-dev-db-esb:1521:esbd"/>
            <property name="hibernate.connection.username" value="CUST_INFO"/>
            <property name="hibernate.connection.password" value="POUND987"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
            <property name="hibernate.c3p0.min_size" value="5"/>
            <property name="hibernate.c3p0.max_size" value="20"/>
            <property name="hibernate.c3p0.timeout" value="300"/>
            <property name="hibernate.c3p0.max_statements" value="50"/>
            <property name="hibernate.c3p0.idle_test_period" value="3000"/>
            <property name="show_sql" value="true"/>
            <property name="format_sql" value="true"/>
            <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
            <property name="hibernate.cache.use_query_cache" value="true"/>
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" /> 
            <property name="hibernate.cache.provider_configuration_file_resource_path" value="ehcache.xml" /> 
        </properties>
    </persistence-unit>
</persistence>

的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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>HibernateWithAnnotation</groupId>
    <artifactId>HibernateWithAnnotation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>HibernateWithAnnotation</name>
    <repositories>
        <repository>
            <id>codelds</id>
            <url>https://code.lds.org/nexus/content/groups/main-repo</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.1-Final</version>

        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.2.0.Final</version>
        </dependency>


        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.5.1-Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.5.1-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>


        <!-- logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.6.1</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>


        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.4.5</version>
        </dependency>


    </dependencies>
</project>

TestAnnotation类

package com.annotation;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class TestAnnotation {
    public static void main(String args[]){
        EntityManagerFactory factory=Persistence.createEntityManagerFactory("annotation");
        EntityManager manager=factory.createEntityManager();
        EntityTransaction transaction=manager.getTransaction();
        transaction.begin();    
        manager.persist(new Message("My Entity Test One More Example New"));
        transaction.commit();
        System.out.println("First time calling Message Object");
        getMessage(manager,23);
        System.out.println("Second time calling Message Object");
        getMessage(manager,23);

        factory.close();
    }


    public static void getMessage(EntityManager manager,long id){
        EntityTransaction transaction1=manager.getTransaction();
        transaction1.begin();   
        Query q=manager.createQuery("from Message m where m.id="+id);
        Message m=(Message)q.getSingleResult();
        System.out.println(m.getMessage_text());
        transaction1.commit();
    }
}

我的问题是:当我通过main方法从TestAnnotation类运行此代码时,我收到以下错误:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named annotation
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    at com.annotation.TestAnnotation.main(TestAnnotation.java:10)

1 个答案:

答案 0 :(得分:1)

您的persistence-unit不完整。请参阅documentation

<class>com.annotation.TestAnnotation</class>添加到persistence-unit节点之前的persistence.xml文件中的properties节点。

transaction-type="RESOURCE_LOCAL"节点上可能还需要persistence-unit

例如,我的工作版使用:

的pom.xml:

     <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.5.4-Final</version>
        <scope>runtime</scope>
     </dependency>

persistenct.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                                 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
   <persistence-unit name="MyPU" transaction-type="RESOURCE_LOCAL">
      <class>com.myentities.MyEntity</class>

      <properties>
         <property name="hibernate.cache.use_query_cache" value="false"/>
         <property name="hibernate.cache.use_second_level_cache" value="false"/>
         <!-- These lines can be used for debugging -->
         <!--<property name="show_sql" value="true"/>-->
         <!--<property name="format_sql" value="true"/>-->
      </properties>
   </persistence-unit>
</persistence>

我的DAO课程:

private EntityManager m_entityManagerFactory;

  // initializer (this is costly, do only 1x on post construct)
  m_entityManager = createEntityManagerFactory( jdbcDriverName, jdbcURL, dbUserName, dbPassword );

  // when needed (less costly, can do 1x or whenever you need the entity manager)
  EntityManager entityManager = m_entityManagerFactory.createEntityManager();

private EntityManagerFactory createEntityManagerFactory (
   @NotNull final String jdbcDriverName,
   @NotNull final String jdbcURL,
   @NotNull final String dbUserName,
   @NotNull final String dbPassword )
{
   final Properties properties = new Properties();
   properties.put( "hibernate.connection.driver_class", jdbcDriverName );
   properties.put( "hibernate.connection.url", jdbcURL );
   properties.put( "hibernate.connection.username", dbUserName );
   properties.put( "hibernate.connection.password", dbPassword );
   return Persistence.createEntityManagerFactory( "AlertProcessorPU", properties );
}