Hibernate ORM 提供程序、Netbeans 7、Glassfish(视频)
我的所有项目中的 Hibernate 基础知识都存在很大问题。
我有 mysql 服务器,并且有一个 hiber 数据库(localhost)。
有两个表:messages 和user。
我有Glassfish 3.1。
我使用 Hibernate Framework 在 Netbeans 7 中启动了新项目。
然后,我创建了实体和 persistence.xml、PersistenceUnit。
之后,我创建了Servlet,即。演示(映射为 /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();
}
}
当我启动 WebApp 时,我发现 EntityManagerFactory 存在问题:
错误:[持久性单元: WebApplication7PU]无法构建 EntityManagerFactory
有什么问题吗?我无法修复它:(
。 。
。 -- 更多信息 --
这是 YouTube 上关于我的问题的视频< /强> .
.
.
.
Glassfish 更新中心:
有 JPA 和 Hibernate 等软件包。
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我在日志中看到:
JNDI 指示 javax.sql.DataSource 所在的位置。你有:
我认为你必须将其更改为:
java:/hiberMysql
First, I see in logs:
JNDI indicates where javax.sql.DataSource is located. You have:
I think you must change this to:
java:/hiberMysql