2013-06-03 65 views
0

Message.java沒有持久性提供EntityManager的命名緩存

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(); 
    } 
} 

我的問題是:當我運行通過的主要方法從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) 
+0

是的,我注意到你打算在以後,我想我沒有儘快刪除評論。但是,您沒有'class'節點。 –

回答

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); 
} 
+0

我像前面說的那樣添加了''節點,但仍然存在問題/錯誤。 –

+0

查閱文檔的第2.2.2章和「Java SE環境中的典型配置」。也許你需要「事務類型」也指定? –

相關問題