【发布时间】:2014-05-05 06:08:16
【问题描述】:
https://github.com/ruoli/SpringHibernateMysql
我正在尝试使用 spring mvc 和 hibernate 实现“一对多”映射。 同时,我的一对一映射运行良好。 但只要我添加另一个实体:
private List<Projects> project
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Main</groupId>
<artifactId>Spring-Hibernate-Mysql</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Spring-Hibernate-Mysql</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.2.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.9.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<finalName>SpringApp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
@OneToMany
private List<Projects> projects;
一切都崩溃了。 这是我的一对多的代码:
员工模型:
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id
@Column(name = "ID", nullable = false)
private String id;
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "AGE", nullable = false)
private long age;
private List<Projects> projects;
public Employee() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getAge() {
return age;
}
public void setAge(long age) {
this.age = age;
}
@OneToMany(mappedBy="employee")
public List<Projects> getProjects() {
return projects;
}
public void setProjects(List<Projects> projects) {
this.projects = projects;
}
}
项目模型:
@Entity
@Table(name = "PROJECTS")
public class Projects {
private Employee employee;
@Column(name = "PROJECT_TITLE", nullable = false)
private String projectTitle;
@Column(name = "PROJECT_TYPE", nullable = false)
private String projectType;
public Projects(){
}
@ManyToOne
@JoinColumn(name = "id")
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public String getProjectTitle() {
return projectTitle;
}
public void setProjectTitle(String projectTitle) {
this.projectTitle = projectTitle;
}
public String getProjectType() {
return projectType;
}
public void setProjectType(String projectType) {
this.projectType = projectType;
}
}
员工道:
public interface EmployeeDAO {
void persistEmployee(Employee employee);
Employee findEmployeeById(String id);
void updateEmployee(Employee employee);
void deleteEmployee(Employee employee);
}
EmployeeDAOImpl:
@Repository("employeeDAO")
public class EmployeeDAOImpl implements EmployeeDAO {
@Autowired
private SessionFactory sessionFactory;
@Override
public void persistEmployee(Employee employee) {
sessionFactory.getCurrentSession().persist(employee);
}
@Override
public Employee findEmployeeById(String id) {
return (Employee) sessionFactory.getCurrentSession().get(Employee.class, id);
}
@Override
public void updateEmployee(Employee employee) {
sessionFactory.getCurrentSession().update(employee);
}
@Override
public void deleteEmployee(Employee employee) {
sessionFactory.getCurrentSession().delete(employee);
}
}
员工服务:
public interface EmployeeService {
void persistEmployee(Employee employee);
Employee findEmployeeById(String id);
void updateEmployee(Employee employee);
void deleteEmployee(Employee employee);
}
EmployeeServiceImpl:
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
EmployeeDAO employeeDAO;
@Override
@Transactional
public void persistEmployee(Employee employee) {
employeeDAO.persistEmployee(employee);
}
@Override
@Transactional
public void updateEmployee(Employee employee) {
employeeDAO.updateEmployee(employee);
}
@Override
@Transactional
public Employee findEmployeeById(String id) {
return employeeDAO.findEmployeeById(id);
}
@Override
@Transactional
public void deleteEmployee(Employee employee) {
employeeDAO.deleteEmployee(employee);
}
}
最后,这是运行程序的主要部分:
public class HelloApp {
public static void main(String[] args) {
System.out.println("load context");
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Projects johnsproject= new Projects();
johnsproject.setId("123");
johnsproject.setProjectTitle("APP project");
johnsproject.setProjectType("IOS");
List<Projects> projectList = new ArrayList<Projects>();
projectList.add(johnsproject);
Employee em = new Employee();
em.setId("88");
em.setName("pj0");
em.setAge(41);
em.setProjects(projectList);
EmployeeService emService = (EmployeeService) context.getBean("employeeService");
emService.persistEmployee(em);
System.out.println("Updated age :" + emService.findEmployeeById("88").getAge());
em.setAge(53);
emService.updateEmployee(em);
System.out.println("Updated age :" + emService.findEmployeeById("88").getAge());
emService.deleteEmployee(em);
context.close();
}
}
还有我的 spring hibernate 配置文件:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<context:component-scan base-package="com.lee.*"/>
<tx:annotation-driven/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:8889/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<list>
<value>com.lee.Model.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
</beans>
这就是它所抱怨的:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.lee.DAO.EmployeeDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring-config.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z
我已经花了一整天的时间试图弄清楚如何使用 spring hibernate 进行一对多映射,epic fail >。
【问题讨论】:
-
java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()- 你的类路径中缺少什么吗? -
刚刚也将 pom.xml 添加到这个问题线程中......这就是我为这个练习项目所拥有的所有代码,我整天都在试图找出问题所在......没有 clud-.-
标签: mysql database spring hibernate mapping