java 错误总结
- junit4 对user实体测试:
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.shop.model.User.packet references an unknown entity: com.shop.model.Packet
错误原因:hibernate.cfg.xml中没有引入packet实体类
添加:<mapping class="com.shop.model.Packet"/> 解决问题
- org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.SingleTableEntityPersister
这次报错的原因是实体类
image.png
对应数据库表名写错了,应该是user,但之前写成User了
下边总结报这个错的可能的几种情况(从网上整理来的):
- 没有导入javassist的jar文件。
- 实体类中有错误。比如@Entity(“User”) 中的表名错误; @Version 注解使用错误等
http://www.reader8.cn/jiaocheng/20130911/2209649.html
http://blog.csdn.net/skyvoice7/article/details/50220667
今天在整合SSH框架时(SpringMVC+Spring+Hibernate) ,在配置完测试时:写了以下代码:
ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.print(ct);
发现运行报错 :
org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
在我踏遍各种论坛,测试多数方法过后,终于给解决了。话不多说,请往下看!
出现这个异常有几个原因:
- 实体类的属性对象没有设置setter或者getter,或者是设置setter或者getter时没有按照Hibernate规范。
2.没有导入javassist的jar文件。
3.实体类中的属性对象名和映射文件的property name不一致。
我这次是因为第二个原因。
导入javassis的jar包就解决了。
3 错误:SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".的报错信息:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
解决方法:再pom.xml依赖中保证下列jar的存在
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>runtime</scope>
</dependency>
log4j学习
http://blog.csdn.net/Try_harder_every_day/article/details/78740767
- 错误:无法初始化log4j,也就是找不到log4j.properties配置文件
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
maven构建的项目默认配置存放路径:src/main/resources/log4j.properties。
官网原文:
Why do I see a warning about "No appenders found for logger" and "Please configure log4j properly"?
This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration. log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system. Knowing the appropriate location to place log4j.properties or log4j.xml requires understanding the search strategy of the class loader in use. log4j does not provide a default configuration since output to the console or to the file system may be prohibited in some environments. Also see FAQ: Why can't log4j find my properties in a J2EE or WAR application?.
- tomcat启动加载时spring初始化bean报错
2018-02-24 11:35:03 ERROR ContextLoader:351 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'AdminDao' defined in file [D:\java\ssh-shop\target\Shop\WEB-INF\classes\com\shop\dao\impl\AdminDaoImpl.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0': Cannot resolve reference to bean 'serviceMethod' while setting bean property 'pointcut'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceMethod': Failed to introspect bean class [org.springframework.aop.aspectj.AspectJExpressionPointcut] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
基本确定错误是<aop:config />的配置错误,所以暂时关闭这个配置
2018-02-24 17:24:10 WARN XmlWebApplicationContext:550 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'AdminDao': Unsatisfied dependency expressed through method 'setSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring/spring-hibernate.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;