您的位置:首页 > 其它

SSH异常解决

2016-11-13 12:31 197 查看
SSH异常整理

一、org.hibernate.lazyinitialzationException:failed to lazily initialize a collection of role:com.xxx.spring.Customer.linkMans could not initialize proxy -no session

延迟初始化加载异常:

错误原因一般有两个:

1.使用hibernate查询时调用了load方法

2.查询某个对象的关联对象的时候。

解决方式:

1.使用get方法(不推荐使用该方式解决该问题,没有延迟加载效果)

2. 在web.xml文件中 解决延迟加载问题的过滤器,加载struts核心过滤器前面

<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>


二、org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘transactionManager’ defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type [java.lang.String] to required type [org.hibernate.SessionFactory] for property ‘sessionFactory’; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.hibernate.SessionFactory] for property ‘sessionFactory’: no matching editors or conversion strategy found

at

创建实体异常:

我出现该问题的原因是在配置事务管理器的时候,将property属性的写成了
<property name="sessionFactory" value="sessionFactory"></property>
,这里要是一个ref(外部连接对象),将value改成ref就没问题了。

三、PropertyNotFoundException:属性找不到异常

Caused by: org.hibernate.PropertyNotFoundException: Could not locate getter method for property [com.xxx.spring.domain.Customer#linkmans]

at org.hibernate.internal.util.ReflectHelper.findGetterMethod(ReflectHelper.java:400)

原因:实体类中的名称大小写不一致

四、QueryParameterException:查询参数异常

org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 2

解决方式:记住Hibernate的SQL—-索引的参数值是从0开始的。

五、GenericJDBCException:通用JDBC异常

org.hibernate.exception.GenericJDBCException: could not execute statement

Caused by: java.sql.SQLException: Field ‘gid’ doesn’t have a default value

原因:外键gid没有默认值,可能在设置主键生成策略没有对应实体类的数据类型

解决方法:如果数据库表中数据类型为Integer,可设置为主键生成策略为increment、native、assign、identity

六、IdentifierGenerationException:标识符产生异常:

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.xxx.domain.Customer

解决方式:主键id一定需要手动设置(可能在映射文件忘记写主键生成策略)

<!-- 主键生成策略 --> <generator class="native"/>


七、ConstraintViolationException:约束违背异常

org.hibernate.exception.ConstraintViolationException: could not execute statement

造成原因:主键冲突,主键已经存在。

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry ‘2-1’ for key ‘PRIMARY’

解决方法:多对多级联查询,没有释放外键维护权 在对应的映射文件里添加 inverse属性为ture

<set name="linkMans" cascade="save-update,delete" inverse="true">
<!-- 多的一方的外键 -->
<key column="lkm_cust_id"/>
<!-- 多的一方的类名全路径 -->
<one-to-many class="com.xxx.domain.LinkMan"/>
</set>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ssh 异常