【hibernate】spring+ jpa + hibername 配置过程遇到的问题

org.springframework.data.jpa.domain.support.AuditingEntityListener

Auditing: 审计;查账
Hades:Hades是一个开源库,基于JPA和Spring构建,通过减少开发工作量显著的改进了数据访问层的实现。

在系统中记录实体类的创建者、修改者和相关日期等信息是一个很常见的需求。Hades提供了监听器EntityListener,可以帮你透明的实现这些功能。要启用Hades的审计功能,只需要在orm.xml文件中定义AuditingEntityListener即可:

 


org.hibernate.tool.hbm2ddl.SchemaUpdate: 201 - HHH000319: Could not get database metadata


nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set


java.sql.SQLException: No suitable driver 没有合适的驱动


nested:嵌套 内装

java.lang.IllegalArgumentException:不合法的参数异常

{
@Entity
@NamedQuery(name = "User.findByTheUsersName", query = "from User u where u.username = ?1")
public class User extends AbstractPersistable<Long>

 


}


<property name="persistenceUnitName" value="jpa.sample"/> 属性
持久化单元名称

“persistenceUnitName”属性,添加“packagesToScan”属性
可以用上面的代替


都去掉后报错:“Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/hibernate-context.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}”

这两个属性貌似任选其一

配置:
<property name="packagesToScan" value="org.springframework.data.jpa.example.repository.simple.*" />
结果:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleUserRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class org.springframework.data.jpa.example.repository.simple.User
配置:
<property name="packagesToScan" value="org.springframework.data.jpa.example.repository.simple" />
结果:
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection

--------------
利用hibernate中的SchemaExport生成数据表

org.hibernate.tool.hbm2ddl.SchemaExport: 385 - HHH000231: Schema export unsuccessful

彻底解决了:

哈哈,问题解决了,为了方便以后遇到这个问题的童鞋们,我还是说下解决方法。
其实是驱动包过时了。
很多人一直都是用的mysql-connector-java-5.0.8.jar这个驱动包,然而hibernate4需要更新的mysql驱动包,现在最新的驱动包是5.1.19,但是在mysql官方任然是5.0.8,需要点击最新版本才能看见5.1.19那个mysql驱动。至于什么方言 MySQL5Dialect ,我看了hibernate4.1.2的devgaide,里面说的根本就不是那么一会事儿,MySQLDialect在hibernate4.1.2里面仍然可以使用哈。

 

org.hibernate.exception.JDBCConnectionException:
数据库未能获得连接

1.检查你的数据库地址:

2.检查用户名:
key="user">
3.检查密码:
key="password">

然后尝试用配置里面的数据连接数据库,看是否正确

如果不正确,看是不是写错了,或者数据库服务未启动。
----------------

org.hibernate.tool.hbm2ddl.SchemaUpdate: Could not complete schema update

其实这个hibernate.hbm2ddl.auto参数的作用主要用于:自动创建|更新|验证数据库表结构。如果不是此方面的需求建议set value="none"。

 

 

 


Could not get database metadata

 


Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection

 


1. org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.UnsupportedOperationException: Not supported by BasicDataSource
Spring不能为JAP创建事务。原因是bean.xml设定了数据源persistence.xml中要把数据库连接去掉
<property name="hibernate.connection.username" value="xxx"/>
<property name="hibernate.connection.password" value="xxx"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/xxx? useUnicode=true&amp;characterEncoding=UTF-8"/>

2. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productServiceBean': Injection of persistence fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [beans.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: org.hibernate.MappingException: Could not determine type for: java.util.Set, for columns: [org.hibernate.mapping.Column(childtypes)]
原因:@Id @GeneratedValue(strategy = GenerationType.AUTO)
这个注解写在get方法上不要写在属性声明上。

3. javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not insert: [com.yufei.bean.product.ProductType]
Caused by: java.sql.SQLException: Data too long for column 'visible' at row 1
原因:实体类字段中的visible插入到数据库中所对应的值为ASCII码味49,超出范围。
换个mysql的jar包解决问题。

4. web容器出现
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
此问题多样化,查阅资料说是jar包commons-logging.jar问题 ,但解决的方式是重新导入项目...

5. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productTypeService': Injection of persistence fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [beans.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V
原因: Spring2.5下asm-2.2.3.jar 与hibernate下的asm.jar 冲突, 删除asm-2.2.3.jar 。

6. Failed to load or instantiate TagLibraryValidator class: org.apache.taglibs.standard.tlv.JstlCoreTLV
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:88)
org.apache.jasper.compiler.Parser.processIncludeDirective(Parser.java:340)
原因:lib中缺少standard.jar , jstl.jar

7. java.lang.NoSuchFieldError: DIRECTORY
at net.fckeditor.connector.impl.AbstractLocalFileSystemConnector.getFolders(AbstractLocalFileSystemConnector.java:140)
at net.fckeditor.connector.Dispatcher.getFoldersAndOrFiles(Dispatcher.java:204)
at net.fckeditor.connector.Dispatcher.doGet(Dispatcher.java:156)
at net.fckeditor.connector.ConnectorServlet.doGet(ConnectorServlet.java:89)
原因:包冲突,删掉struts1.3自带的commons-io-1.1.jar,commons-fileupload-1.1.1.jar,加入commons-io-1.3.2.jar,commons-fileupload-1.2.1.jar

8.IOException while loading persisted sessions: java.io.EOFException
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2281)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
原因是tomcat对硬盘的session读取失败,彻底解决办法一下:将work下面的文件清空,主要是*.ser文件,或者只是删除掉session.ser即可以解决。

9.javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.dalton.domain.User
org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException
原因:(1) 是entity bean的 @Id标记错了,标记在了String 等类型上
(2) 对id字段手工设定了值。

10 org.springframework.web.context.ContextLoader(line:215) - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'brand' defined in file [D:\Tomcat6\webapps\autoet\WEB-INF\classes\com\autoet\
action\brand\BrandAction.class]: Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Could not instantiate bean
class [com.autoet.action.brand.BrandAction]: Constructor threw exception; nested
exception is java.lang.NullPointerException
原因:项目使用了<context:component-scan/>扫描机制, 而类BrandAction没有标注为原型,
为类添加 @Scope("prototype")标注(与@Controller相同位置)

11 org.hibernate.tool.hbm2ddl.SchemaUpdate(line:222) - could not complete schema update
java.lang.UnsupportedOperationException: Not supported by BasicDataSource
原因 spring.xml 与 persistence.xml中都配置了数据库连接,注释掉persistence.xml中的连接信息

12 org.apache.struts2.dispatcher.Dispatcher(line:27) - Dispatcher initialization failed
Unable to load configuration. - [unknown location]
Cannot locate the chosen ObjectFactory implementation: spring - [unknown location]
原因 struts.xml中使用了<constant name="struts.objectFactory" value="spring"/>,将其注释掉

posted on 2015-08-06 15:16  viewcozy  阅读(2558)  评论(0编辑  收藏  举报