Hibernate ORM Provider, Netbeans 7, Glassfish (video)

随声附和 提交于 2019-12-13 18:37:02

问题


I have big problem with basic of the Hibernate in all my projects.

I have mysql server and there are a hiber database (localhost).
There are two tables: messages and user.

I have Glassfish 3.1.

I started new project in Netbeans 7 with Hibernate Framework.
Then, I created entities and persistence.xml, PersistenceUnit.

After that, I created Servlet, ie. Demo (mapped as /Demo).

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

When I started my WebApp, I saw problem with EntityManagerFactory:

error: [PersistenceUnit: WebApplication7PU] Unable to build EntityManagerFactory

What is a problem? I can't fix it :(
. .

. -- MORE INFO --

This is a video on youtube with my problem .
.
.
.
Glassfish update center:
There are packages like JPA and Hibernate.

Glassfish log:

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:


First, I see in logs:

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

JNDI indicates where javax.sql.DataSource is located. You have:

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

I think you must change this to: java:/hiberMysql



来源:https://stackoverflow.com/questions/6121865/hibernate-orm-provider-netbeans-7-glassfish-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!