2011-06-17 68 views
1

我試圖使用JPA/Hibernate映射我的數據庫(jira數據庫)。 但是當我這樣做!(我曾嘗試在另一個數據庫相同的配置和它的工作)的錯誤occure 我cofiguration文件:當我嘗試映射(堅持)我的數據庫時出錯

的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" 
    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"> 
    <persistence-unit name="JIRAKPIPU" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <class>DTOJIRA.Portalpage</class> 
     <class>DTOJIRA.QrtzFiredTriggers</class> 
     <class>DTOJIRA.Jiraworkflows</class> 
     <class>DTOJIRA.Externalgadget</class> 
     <class>DTOJIRA.OsHistorystepPrev</class> 
     <class>DTOJIRA.Versioncontrol</class> 
     <class>DTOJIRA.CwdApplicationAddress</class> 
     <class>DTOJIRA.Oauthspconsumer</class> 
     <class>DTOJIRA.Label</class> 
     <class>DTOJIRA.Propertydata</class> 
     <class>DTOJIRA.ExternalEntities</class> 
     <class>DTOJIRA.Changeitem</class> 
     <class>DTOJIRA.Upgradehistory</class> 
     <class>DTOJIRA.Favouriteassociations</class> 
     <class>DTOJIRA.Jiraaction</class> 
     <class>DTOJIRA.Issuestatus</class> 
     <class>DTOJIRA.Priority</class> 
     <class>DTOJIRA.Serviceconfig</class> 
     <class>DTOJIRA.QrtzJobDetails</class> 
     <class>DTOJIRA.Permissionscheme</class> 
     <class>DTOJIRA.Userhistoryitem</class> 
     <class>DTOJIRA.Oauthconsumertoken</class> 
     <class>DTOJIRA.QrtzJobListeners</class> 
     <class>DTOJIRA.Fieldlayoutscheme</class> 
     <class>DTOJIRA.Jiraeventtype</class> 
     <class>DTOJIRA.Genericconfiguration</class> 
     <class>DTOJIRA.Searchrequest</class> 
     <class>DTOJIRA.Configurationcontext</class> 
     <class>DTOJIRA.Changegroup</class> 
     <class>DTOJIRA.Customfieldoption</class> 
     <class>DTOJIRA.Customfield</class> 
     <class>DTOJIRA.OsCurrentstepPrev</class> 
     <class>DTOJIRA.Userassociation</class> 
     <class>DTOJIRA.Remembermetoken</class> 
     <class>DTOJIRA.Userbase</class> 
     <class>DTOJIRA.Fieldconfigscheme</class> 
     <class>DTOJIRA.Jiraissue</class> 
     <class>DTOJIRA.CwdDirectoryOperation</class> 
     <class>DTOJIRA.Issuetypescreenscheme</class> 
     <class>DTOJIRA.Gadgetuserpreference</class> 
     <class>DTOJIRA.Filtersubscription</class> 
     <class>DTOJIRA.Optionconfiguration</class> 
     <class>DTOJIRA.Projectroleactor</class> 
     <class>DTOJIRA.Fieldscreen</class> 
     <class>DTOJIRA.Propertydate</class> 
     <class>DTOJIRA.Oauthsptoken</class> 
     <class>DTOJIRA.QrtzCronTriggers</class> 
     <class>DTOJIRA.Schemeissuesecuritylevels</class> 
     <class>DTOJIRA.Upgradeversionhistory</class> 
     <class>DTOJIRA.Projectversion</class> 
     <class>DTOJIRA.CwdDirectory</class> 
     <class>DTOJIRA.Fieldscreenlayoutitem</class> 
     <class>DTOJIRA.Fieldscreenschemeitem</class> 
     <class>DTOJIRA.Notificationscheme</class> 
     <class>DTOJIRA.Issuelinktype</class> 
     <class>DTOJIRA.OsWfentry</class> 
     <class>DTOJIRA.Fieldscreenscheme</class> 
     <class>DTOJIRA.Notification</class> 
     <class>DTOJIRA.Issuesecurityscheme</class> 
     <class>DTOJIRA.Schemeissuesecurities</class> 
     <class>DTOJIRA.Jiradraftworkflows</class> 
     <class>DTOJIRA.CwdApplication</class> 
     <class>DTOJIRA.Jiraperms</class> 
     <class>DTOJIRA.QrtzSimpleTriggers</class> 
     <class>DTOJIRA.Fieldscreentab</class> 
     <class>DTOJIRA.QrtzTriggers</class> 
     <class>DTOJIRA.Component</class> 
     <class>DTOJIRA.Issuelink</class> 
     <class>DTOJIRA.Trustedapp</class> 
     <class>DTOJIRA.Fileattachment</class> 
     <class>DTOJIRA.Propertydecimal</class> 
     <class>DTOJIRA.Propertystring</class> 
     <class>DTOJIRA.Avatar</class> 
     <class>DTOJIRA.Project</class> 
     <class>DTOJIRA.CwdUser</class> 
     <class>DTOJIRA.Membershipbase</class> 
     <class>DTOJIRA.CwdDirectoryAttribute</class> 
     <class>DTOJIRA.Workflowschemeentity</class> 
     <class>DTOJIRA.Notificationinstance</class> 
     <class>DTOJIRA.Fieldconfigschemeissuetype</class> 
     <class>DTOJIRA.Schemepermissions</class> 
     <class>DTOJIRA.Projectrole</class> 
     <class>DTOJIRA.Issuetype</class> 
     <class>DTOJIRA.Columnlayoutitem</class> 
     <class>DTOJIRA.Groupbase</class> 
     <class>DTOJIRA.Columnlayout</class> 
     <class>DTOJIRA.Mailserver</class> 
     <class>DTOJIRA.Sharepermissions</class> 
     <class>DTOJIRA.Propertyentry</class> 
     <class>DTOJIRA.Oauthconsumer</class> 
     <class>DTOJIRA.CwdMembership</class> 
     <class>DTOJIRA.SequenceValueItem</class> 
     <class>DTOJIRA.TrackbackPing</class> 
     <class>DTOJIRA.Fieldlayout</class> 
     <class>DTOJIRA.Customfieldvalue</class> 
     <class>DTOJIRA.Propertynumber</class> 
     <class>DTOJIRA.Workflowscheme</class> 
     <class>DTOJIRA.Listenerconfig</class> 
     <class>DTOJIRA.QrtzTriggerListeners</class> 
     <class>DTOJIRA.Portletconfiguration</class> 
     <class>DTOJIRA.Projectcategory</class> 
     <class>DTOJIRA.Fieldlayoutitem</class> 
     <class>DTOJIRA.Pluginversion</class> 
     <class>DTOJIRA.OsHistorystep</class> 
     <class>DTOJIRA.CwdGroup</class> 
     <class>DTOJIRA.CwdGroupAttributes</class> 
     <class>DTOJIRA.Fieldlayoutschemeassociation</class> 
     <class>DTOJIRA.Issuetypescreenschemeentity</class> 
     <class>DTOJIRA.Fieldconfiguration</class> 
     <class>DTOJIRA.Fieldlayoutschemeentity</class> 
     <class>DTOJIRA.Propertytext</class> 
     <class>DTOJIRA.OsCurrentstep</class> 
     <class>DTOJIRA.Resolution</class> 
     <class>DTOJIRA.QrtzCalendars</class> 
     <class>DTOJIRA.Worklog</class> 
     <class>DTOJIRA.Nodeassociation</class> 
     <class>DTOJIRA.CwdUserAttributes</class> 
     <exclude-unlisted-classes>false</exclude-unlisted-classes> 
     <properties> 
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jiradb" /> 
      <property name="javax.persistence.jdbc.password" value="root" /> 
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> 
      <property name="javax.persistence.jdbc.user" value="root" /> 
     </properties> 
    </persistence-unit> 
</persistence> 

**hibernate.cfg.xml** 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jiradb</property> 
     <property name="hibernate.connection.username">root</property> 
     <property name="hibernate.connection.password">root</property> 
    </session-factory> 
</hibernate-configuration> 

我的班級閱讀:

public class JiraissueRead { 
    EntityManagerFactory emf = Persistence 
      .createEntityManagerFactory("JIRAKPIPU"); 
    EntityManager em = emf.createEntityManager(); 

    public JiraissueRead() { 
    } 

    public Jiraissue findKpi(Integer id) { 
     System.out.println("ttttttttttttttttttttttttttttttttt"); 

     try { 

      EntityTransaction entr = em.getTransaction(); 
      System.out.println("debut 1"); 
      entr.begin(); 
      entr.commit(); 
      System.out.println("debut 2"); 
      Jiraissue j = em.find(Jiraissue.class, id); 
      return j; 
     } catch (Exception e) { 
      System.out.println(e.getMessage()); 
     } finally { 
      em.close(); 
     } 
     return null; 
    } 
} 

主:

public static void main(String[] args) { 
    // TODO code application logic here 
    JiraissueRead jiraissue = new JiraissueRead(); 
    System.out.println("_______________"); 
    System.out.println(jiraissue.findKpi(10000)); 

} 

錯誤

17 juin 2011 17:39:12 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findById => SELECT c FROM CwdUserAttributes c WHERE c.id = :id 
17 juin 2011 17:39:12 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findByUserId => SELECT c FROM CwdUserAttributes c WHERE c.userId = :userId 
17 juin 2011 17:39:12 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findByDirectoryId => SELECT c FROM CwdUserAttributes c WHERE c.directoryId = :directoryId 
17 juin 2011 17:39:12 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findByAttributeName => SELECT c FROM CwdUserAttributes c WHERE c.attributeName = :attributeName 
17 juin 2011 17:39:12 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findByAttributeValue => SELECT c FROM CwdUserAttributes c WHERE c.attributeValue = :attributeValue 
17 juin 2011 17:39:12 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findByLowerAttributeValue => SELECT c FROM CwdUserAttributes c WHERE c.lowerAttributeValue = :lowerAttributeValue 
17 juin 2011 17:39:12 org.hibernate.cfg.annotations.EntityBinder bindTable 
INFO: Bind entity DTOJIRA.CwdUserAttributes on table cwd_user_attributes 
17 juin 2011 17:39:12 org.hibernate.cfg.AnnotationConfiguration secondPassCompile 
INFO: Hibernate Validator not found: ignoring 
    17 juin 2011 17:39:12 org.hibernate.connection.UserSuppliedConnectionProvider configure 
ATTENTION: No connection properties specified - the user must supply JDBC connections 
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: J IRAKPIPU] Unable to build EntityManagerFactory 
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677) 
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory (HibernatePersistence.java:126) 
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51) 
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33) 
at DAOJIRA.JiraissueRead.<init>(JiraissueRead.java:16) 
at DAOJIRA.Main.main(Main.java:18) 
Caused by: org.hibernate.HibernateException: Hibernate Dialect must be explicitly set 
at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57) 
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39) 
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:426) 
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:128) 
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292) 
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory (AnnotationConfiguration.java:859) 
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669) 
... 5 more 
Java Result: 1 

後我加入而且hibernate.dialect我的persistence.xml和錯誤改變,知道錯誤的是(錯誤太長了):

INFO: Binding Named query: Nodeassociation.findAll => SELECT n FROM Nodeassociation n 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: Nodeassociation.findBySourceNodeId => SELECT n FROM  Nodeassociation n WHERE n.nodeassociationPK.sourceNodeId = :sourceNodeId 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: Nodeassociation.findBySourceNodeEntity => SELECT n FROM  Nodeassociation n WHERE n.nodeassociationPK.sourceNodeEntity = :sourceNodeEntity 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: Nodeassociation.findBySinkNodeId => SELECT n FROM Nodeassociation n WHERE n.nodeassociationPK.sinkNodeId = :sinkNodeId 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: Nodeassociation.findBySinkNodeEntity => SELECT n FROM Nodeassociation n WHERE n.nodeassociationPK.sinkNodeEntity = :sinkNodeEntity 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: Nodeassociation.findByAssociationType => SELECT n FROM Nodeassociation n WHERE n.nodeassociationPK.associationType = :associationType 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: Nodeassociation.findBySequence => SELECT n FROM Nodeassociation n WHERE n.sequence = :sequence 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.EntityBinder bindTable 
INFO: Bind entity DTOJIRA.Nodeassociation on table nodeassociation 
18 juin 2011 09:49:46 org.hibernate.cfg.AnnotationBinder bindClass 
INFO: Binding entity from annotated class: DTOJIRA.CwdUserAttributes 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findAll => SELECT c FROM CwdUserAttributes c 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findById => SELECT c FROM CwdUserAttributes c WHERE c.id = :id 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findByUserId => SELECT c FROM CwdUserAttributes c WHERE c.userId = :userId 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findByDirectoryId => SELECT c FROM CwdUserAttributes c WHERE c.directoryId = :directoryId 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findByAttributeName => SELECT c FROM CwdUserAttributes c WHERE c.attributeName = :attributeName 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findByAttributeValue => SELECT c FROM CwdUserAttributes c WHERE c.attributeValue = :attributeValue 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.QueryBinder bindQuery 
INFO: Binding Named query: CwdUserAttributes.findByLowerAttributeValue => SELECT c FROM CwdUserAttributes c WHERE c.lowerAttributeValue = :lowerAttributeValue 
18 juin 2011 09:49:46 org.hibernate.cfg.annotations.EntityBinder bindTable 
INFO: Bind entity DTOJIRA.CwdUserAttributes on table cwd_user_attributes 
18 juin 2011 09:49:46 org.hibernate.cfg.AnnotationConfiguration secondPassCompile 
INFO: Hibernate Validator not found: ignoring 
18 juin 2011 09:49:47 org.hibernate.connection.UserSuppliedConnectionProvider configure 
ATTENTION: No connection properties specified - the user must supply JDBC connections 
juin 2011 09:49:47 org.hibernate.dialect.Dialect <init> 
INFO: Using dialect: org.hibernate.dialect.MySQLDialect 
18 juin 2011 09:49:47 org.hibernate.transaction.TransactionFactoryFactory  buildTransactionFactory 
INFO: Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory 
18 juin 2011 09:49:47 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup 
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended) 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Automatic flush during beforeCompletion(): disabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Automatic session close at end of transaction: disabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Scrollable result sets: disabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: JDBC3 getGeneratedKeys(): disabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Connection release mode: auto 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Maximum outer join fetch depth: 2 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Default batch fetch size: 1 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Generate SQL with comments: disabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Order SQL updates by primary key: disabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Order SQL inserts for batching: disabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory 
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory 
18 juin 2011 09:49:47 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init> 
INFO: Using ASTQueryTranslatorFactory 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Query language substitutions: {} 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: JPA-QL strict compliance: enabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Second-level cache: enabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Query cache: disabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory createCacheProvider 
INFO: Cache provider: org.hibernate.cache.NoCacheProvider 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Optimize cache for minimal puts: disabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Structured second-level cache entries: disabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Statistics: disabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Deleted entity synthetic identifier rollback: disabled 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Default entity-mode: pojo 
18 juin 2011 09:49:47 org.hibernate.cfg.SettingsFactory buildSettings 
INFO: Named query checking : enabled 
18 juin 2011 09:49:47 org.hibernate.impl.SessionFactoryImpl <init> 
INFO: building session factory 
18 juin 2011 09:49:47 org.hibernate.impl.SessionFactoryObjectFactory addInstance 
INFO: Not binding factory to JNDI, no JNDI name configured 
_______________ 
ttttttttttttttttttttttttttttttttt 
debut 1 
The user must supply a JDBC connection 
errrrrs 
++++++++++null 
BUILD SUCCESSFUL (total time: 5 seconds) 

回答

3

Hibernate EntityManager用作JPA提供程序時使用的方言可以在persistence.xml文件中配置爲屬性:

<persistence-unit> 
    <properties> 
     ... 
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> 
     ... 
    </properties> 
</persistence-unit> 

將Hibernate配置爲用作JPA 2.0提供程序。

按照Hibernate EntityManager文檔,在使用JPA的環境中,不需要使用hibernate-cfg.xml文件。所有屬性都可以在persistence.xml文件中指定,這些屬性也可以包含Hibernate本機的屬性。

+0

你好,謝謝你的回覆。我已將hibernate.dialect添加到我的persistence.xml中,並更改了錯誤,知道錯誤是:注意:沒有指定連接屬性用戶必須提供JDBC連接 org.hibernate.dialect.Dialect 信息:使用方言:org .hibernate.dialect.MySQLDialect org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory org.hibernate.transaction.JDBCTransactionFactory INFO:未結合工廠JNDI,沒有JNDI名稱配置 ___The用戶必須提供的JDBC連接 犯錯 ++空 – rym

+0

@rym,我想看看更新persistence.xml和hibernate.cfg.xml文件。 –

+0

@rym,我不明白爲什麼你應該同時有一個hibernate-cfg.xml和一個persistence.xml文件。就像我在答案中所說的那樣,你可以在persistence.xml文件中配置hibernate.dialect屬性,正在使用的屬性名稱不是來自「javax.persistence。*」空間,所以如果適用,它將被拾取。我認爲應該放棄使用hibernate-cfg.xml文件是安全的,因爲它不起任何作用。 –

相關問題