学SSH真郁闷,有时候错误会莫名奇妙,比如前些天我把<property name=hbm2ddl.auto>update</property>改为code=Java]<property name=hibernate.hbm2ddl.auto>update</property>[[/code],一个一直报错的项目就不错了,那个郁闷啊....
开始测试没问题,然后猛的初始化Spring的容器都报错,就是ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");这一句。现在这个错误是:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'proService': Injection of resource methods failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:275)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1030)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
.....
我的bean.xml文件:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.rh"/>      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:jdbc.properties</value>
    </property>
</bean><bean id="dataSource" destroy-method="close"
      class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean><bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.rh.bean.product.ProductType</value>
</list>
</property>
<property name="hibernateProperties">
<props>
     <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
     <prop key="current_session_context_class">thread</prop>
     <prop key="show_sql">true</prop>
     <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean></beans>用到的实体类的代码:package com.rh.bean.product;import java.util.HashSet;
import java.util.Set;import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;@Entity
public class ProductType {

private Integer typeid;
/**类别名称*/
private String name;
/**在google上 的描述*/
private String note;
/**是否可见*/
private Boolean visible=true;
/**得到所有子类别*/
private Set<ProductType> childtypes=new HashSet<ProductType>();
/**所属父类*/
private ProductType parent;

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getTypeid() {
return typeid;
}
public void setTypeid(Integer typeid) {
this.typeid = typeid;
}

@Column(length=36,nullable=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Column(length=100)
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}

@Column(nullable=true)
public Boolean getVisible() {
return visible;
}
public void setVisible(Boolean visible) {
this.visible = visible;
}

@OneToMany(cascade={CascadeType.REMOVE,CascadeType.REFRESH})
public Set<ProductType> getChildtypes() {
return childtypes;
}
public void setChildtypes(Set<ProductType> childTypes) {
this.childtypes = childTypes;
}

@ManyToOne(optional=true,cascade=CascadeType.REFRESH)
@JoinColumn(name="parentid")
public ProductType getParent() {
return parent;
}
public void setParent(ProductType parent) {
this.parent = parent;
}
}
,请大家帮忙找下错误啊。谢谢

解决方案 »

  1.   

    仔细看看这个 proService  bean  里面的映射关系
      

  2.   

    我直接用的Annotation啊,全部映射在上面了。开始都没错,后来就报错了,自动配置了那个@OneToMany后,求解决
      

  3.   

    我直接用的Annotation啊...
    那你还是自己慢慢检查吧》。。这个不好帮你了
      

  4.   


    就是 nested exception is java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z
        这一句,一直这一个错误。
      

  5.   

    你给OneToMany配上Class,指明是那个class
      

  6.   

    唉,包冲突,不说了,hibernate 3.5.6final版本里的JPA-apifinal.jar和Myelcipse里面的J2EE的jar包冲突.
      

  7.   

    可以看一下这个:http://blog.csdn.net/lingwing/article/details/7108351