2011-05-25 74 views
1

我在我的所有项目中都遇到了基本的Hibernate问题。休眠ORM提供程序,Netbeans 7,Glassfish(视频)

我有mysql服务器,并有一个hiber数据库(本地主机)。
有两个表:消息用户。我有Glassfish 3.1

我开始了新项目Netbeans 7Hibernate Framework
然后,我创建了实体和persistence.xml,PersistenceUnit。

之后,我创建了Servlet,即。演示(映射为/演示)。

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = response.getWriter(); 
     try { 

      out.println("<html>"); 
      out.println("<head>"); 
      out.println("<title>Servlet Demo</title>"); 
      out.println("</head>"); 
      out.println("<body>"); 
      out.println("<h1>Servlet Demo at " + request.getContextPath() + "</h1>"); 

      EntityManagerFactory emf = 
         Persistence.createEntityManagerFactory("WebApplication7PU"); 
      EntityManager em = emf.createEntityManager(); 

      em.getTransaction().begin(); 


      Message m = new Message(Integer.SIZE, 0, "Hello world"); 
      em.persist(m); 


      em.getTransaction().commit(); 

      out.println("</body>"); 
      out.println("</html>"); 
     } 
     catch (Exception ex){ 
      out.println("error: "+ex.getMessage()); 
     } 
     finally {    
      out.close(); 
     } 
    } 

当我开始我的Web应用程序,我看到了EntityManagerFactory的问题:

error: [PersistenceUnit: WebApplication7PU] Unable to build EntityManagerFactory

什么是问题?我不能修复它:(

- 更多信息 -

This is a video on youtube with my problem



GlassFish更新中心。。。。。:
有像JPA和休眠包等。

Glassfish的日志:

INFO: Hibernate Validator not found: ignoring 
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. 
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. 
INFO: Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled. 
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. 
INFO: JNDI InitialContext properties:{} 
INFO: Using datasource: hiberMysql 
INFO: RDBMS: MySQL, version: 5.1.53-community-log 
INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.13 (Revision: ${bzr.revision-id}) 
INFO: Using dialect: org.hibernate.dialect.MySQLDialect 
INFO: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 
INFO: Transaction strategy: org.hibernate.ejb.transaction.JoinableCMTTransactionFactory 
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended) 
INFO: Automatic flush during beforeCompletion(): disabled 
INFO: Automatic session close at end of transaction: disabled 
INFO: JDBC batch size: 15 
INFO: JDBC batch updates for versioned data: disabled 
INFO: Scrollable result sets: enabled 
INFO: JDBC3 getGeneratedKeys(): enabled 
INFO: Connection release mode: auto 
INFO: Maximum outer join fetch depth: 2 
INFO: Default batch fetch size: 1 
INFO: Generate SQL with comments: disabled 
INFO: Order SQL updates by primary key: disabled 
INFO: Order SQL inserts for batching: disabled 
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory 
INFO: Using ASTQueryTranslatorFactory 
INFO: Query language substitutions: {} 
INFO: JPA-QL strict compliance: enabled 
INFO: Second-level cache: enabled 
INFO: Query cache: disabled 
INFO: Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory 
INFO: Optimize cache for minimal puts: disabled 
INFO: Structured second-level cache entries: disabled 
INFO: Statistics: disabled 
INFO: Deleted entity synthetic identifier rollback: disabled 
INFO: Default entity-mode: pojo 
INFO: Named query checking : enabled 
INFO: Check Nullability in Core (should be disabled when Bean Validation is on): disabled 
INFO: building session factory 
INFO: Not binding factory to JNDI, no JNDI name configured 

Persistnce.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="WebApplication7PU" transaction-type="JTA"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <jta-data-source>hiberMysql</jta-data-source> 
    <exclude-unlisted-classes>false</exclude-unlisted-classes> 
    <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/hiber</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password">haslo</property> 
    </session-factory> 
</hibernate-configuration> 
+1

: java吗?因为你的persistence.xml建议一个名为hiberMySQL的数据源,但这不是你的Hibernate配置建议的。 – 2011-05-25 18:38:45

+0

你能否详细说明一下? – vizzdoom 2011-05-25 22:15:13

+1

首先,我会看看Spring处理您的交易,因为您的代码中存在一个错误,如果您抛出异常,则不会回滚事务,而只是将其保持打开状态。你的捕获或最终应该处理它。以下是一段视频,展示了Jpa和Spring的最佳实践和设置:http://pluralsight.com/training/Courses/TableOfContents/spring-jpa-hibernate – bh5k 2013-04-21 13:10:14

回答

1

首先,我在日志中看到:

INFO: Not binding factory to JNDI, no JNDI name configured 

JNDI指示a javax.sql.DataSource所在的位置。您有:

<jta-data-source>hiberMysql</jta-data-source> 

我想你必须把它改成:您使用的数据源或您的休眠文件的配置参数/ hiberMysql

+0

谢谢,这是JNDI的问题。 – vizzdoom 2011-10-19 23:16:20