如何使用Hibernate注释@ManyToOne和@OneToMany进行关联(How to use Hibernate annotations @ManyToOne and @OneToMany for associations)
我正在使用本教程学习Spring,Hibernate,Maven: Chad Lung:一个使用Netbeans 7,JUnit,Maven,HSQLDB,Spring和Hibernate的项目 。 它工作正常,但我需要建立一对多的关系(一名员工有很多任务)。 我尝试了很多例子,但仍然无法理解如何让我的代码工作:
Employee.java:
package com.giantflyingsaucer.simplespringhibernate.entity; import javax.persistence.*; import java.io.Serializable; import java.util.List; @Entity @Table(name = "Employees") public class Employee implements Serializable { private Integer employeeId; private List<Task> tasks; @Id @Column(name = "idEmployees", nullable=false) public Integer getEmployeeId() { return this.employeeId; } public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } @OneToMany(fetch = FetchType.LAZY) @JoinColumn(name="idEmployees") public List<Task> getTasks() { return tasks; } }
Task.java:
package com.giantflyingsaucer.simplespringhibernate.entity; import javax.persistence.*; import java.io.Serializable; @Entity @Table(name = "Tasks") public class Task implements Serializable { private Integer taskId; private Employee employee; @Id @Column(name = "idTasks", nullable=false) public Integer getTaskId() { return this.taskId; } public void setTaskId(Integer taskId) { this.taskId = taskId; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "TasksIdEmployees") public Employee getEmployee() {return employee;} }
DB-config.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>${jdbc.driver.className}</value> </property> <property name="jdbcUrl"> <value>${jdbc.url}</value> </property> <property name="user"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="packagesToScan" value="com.giantflyingsaucer.simplespringhibernate.entity" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">false</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <tx:annotation-driven />
MySQL表格:
CREATE TABLE employees ( `idEmployees` int(11) NOT NULL, PRIMARY KEY (`idEmployees`) ); CREATE TABLE tasks ( `idTasks` int(11) NOT NULL, `TasksIdEmployees` int(11) DEFAULT NULL, PRIMARY KEY (`idTasks`), KEY `FkTasksEmployees_idx` (`TasksIdEmployees`), CONSTRAINT `FkTasksEmployees` FOREIGN KEY (`TasksIdEmployees`) REFERENCES `employees` (`idEmployees`) ON DELETE NO ACTION ON UPDATE NO ACTION );
非常感谢!
通过在NetBeans中自动生成映射文件和POJO,我找到了答案:
// Employee.java: @OneToMany(fetch = FetchType.LAZY, mappedBy = "employees") public List<Task> getTasks() { return this.tasks; } public void setTasks(List<Task> tasks) { this.tasks = tasks; } // Task.java: @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "TasksIdEmployees") public Employee getEmployees() { return this.employee; } public void setEmployees(Employee employee) { this.employee = employee; }
I am learning Spring, Hibernate, Maven by using this tutorial: Chad Lung: A project using Netbeans 7, JUnit, Maven, HSQLDB, Spring and Hibernate. It works ok but I need to make one-to-many relationship (one Employee have many Tasks). I have tried many examples but still can't get idea how to make my code work:
Employee.java:
package com.giantflyingsaucer.simplespringhibernate.entity; import javax.persistence.*; import java.io.Serializable; import java.util.List; @Entity @Table(name = "Employees") public class Employee implements Serializable { private Integer employeeId; private List<Task> tasks; @Id @Column(name = "idEmployees", nullable=false) public Integer getEmployeeId() { return this.employeeId; } public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } @OneToMany(fetch = FetchType.LAZY) @JoinColumn(name="idEmployees") public List<Task> getTasks() { return tasks; } }
Task.java:
package com.giantflyingsaucer.simplespringhibernate.entity; import javax.persistence.*; import java.io.Serializable; @Entity @Table(name = "Tasks") public class Task implements Serializable { private Integer taskId; private Employee employee; @Id @Column(name = "idTasks", nullable=false) public Integer getTaskId() { return this.taskId; } public void setTaskId(Integer taskId) { this.taskId = taskId; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "TasksIdEmployees") public Employee getEmployee() {return employee;} }
db-config.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>${jdbc.driver.className}</value> </property> <property name="jdbcUrl"> <value>${jdbc.url}</value> </property> <property name="user"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="packagesToScan" value="com.giantflyingsaucer.simplespringhibernate.entity" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">false</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <tx:annotation-driven />
MySQL tables:
CREATE TABLE employees ( `idEmployees` int(11) NOT NULL, PRIMARY KEY (`idEmployees`) ); CREATE TABLE tasks ( `idTasks` int(11) NOT NULL, `TasksIdEmployees` int(11) DEFAULT NULL, PRIMARY KEY (`idTasks`), KEY `FkTasksEmployees_idx` (`TasksIdEmployees`), CONSTRAINT `FkTasksEmployees` FOREIGN KEY (`TasksIdEmployees`) REFERENCES `employees` (`idEmployees`) ON DELETE NO ACTION ON UPDATE NO ACTION );
Thanks a lot!
I found an answer by autogenerating Mapping files and POJOs in NetBeans:
// Employee.java: @OneToMany(fetch = FetchType.LAZY, mappedBy = "employees") public List<Task> getTasks() { return this.tasks; } public void setTasks(List<Task> tasks) { this.tasks = tasks; } // Task.java: @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "TasksIdEmployees") public Employee getEmployees() { return this.employee; } public void setEmployees(Employee employee) { this.employee = employee; }
原文:https://stackoverflow.com/questions/16511237
最满意答案
我是学服装设计的,对于克莱凯蒂了解一点。ClarecKatie是一个英国时尚皮具品牌,在百度上可以查得到 。
其他回答
我关注的一个时尚博主推荐过,我就去查了一下发现卖的好。克莱凯蒂(clareckatie)在年轻人的圈子里很火。我前几天还在万达入手了一个包,时尚新颖质量好。
相关问答
更多-
计算机行业发展前景怎么样?[2023-10-27]
钱不好赚啊,竞争太激烈,和打工的差不多 -
SANDEX这个瑞士品牌手表哪里有卖?[2022-11-12]
去一些大型的商场就有的吧,这个应该也是个瑞士品牌来的,所以应该有专柜的。 -
VIM品牌是哪的?怎么样啊,大家知道不?[2019-05-20]
你好,伟恩是新加坡的 VIM伟恩定位于时尚、潮流与品位,表达了对个性(Versatility)、创新(Innovation)和多元文化(Multiculturalism)的追求的品牌理念。VIM伟恩是极具创新精神和充满设计感的品牌,致力创新、尊重原创,以创新和设计形成VIM伟恩的独特性和差异化,塑造VIM伟恩创新、时尚的精品品牌形象。 http://tech.sina.com.cn/mobile/n/2010-01-09/00011207946.shtml -
加拿大户外品牌——stormtech(风暴)这个品牌怎么样![2021-09-25]
STORMTECH是来自加拿大的私人控股公司,成立于1977年。时至今日,它已经从一个小型服装销售公司发展成为一个全球公认的知名品牌服饰公司,产品远销美国,欧洲,澳大利亚和新西兰。公司专注于研发、生产、销售高品质的功能性外衣,服饰,箱包及配件。所有这些产品都是以 STORMTECH品牌进行销售。 STORMTECH作为一个知名的户外品牌,加拿大的始祖鸟在中国已经占据了不小的市场份额,有众多的驴友喜欢它的产品。今年,加拿大的另一个户外品牌stormtech(风暴)也将进入中国户外市场。 stormte ... -
求教SantaCruz 这品牌车架怎么样[2022-04-06]
santa cruz 是美国品牌,虽然他的车架现在大都是台湾产,但依旧是世界上最顶级的品牌之一,santacruz车架每年产量很小,质量上乘,价格昂贵。算是顶尖小众的一个品牌。 santa cruz的明星车架,除了为其打下品牌基础的superlight(xc级别的软尾)外,现在的明星车架是碳纤维的V10,其产品线覆盖了从硬尾、不同后轮行程的软尾乃至极限坠山赛的各款车架。 中文名戏称为:三条裤子。 -
lancel是个什么样的品牌[2022-11-19]
在法国也就是个比较平民的牌子,比LONGCHAMP好点 打折季一般都是5折 LANCL的水桶包应该是卖得最好的! -
ClarecKatie这个品牌怎么样?[2023-04-25]
我是学服装设计的,对于克莱凯蒂了解一点。ClarecKatie是一个英国时尚皮具品牌,在百度上可以查得到 。 -
iry是什么样的品牌?[2022-06-01]
IRY是现在挺多人在用的一个护肤品牌,听说效果很不错,我也准备买一款面膜试试 -
外星人品牌笔记本电脑怎么样[2023-06-18]
首先看你使用它做什么了,如果是玩电竞游戏绰绰有余。 但是性价比没有神舟电脑高。(外星人是戴尔电脑的游戏高端产品) 如果像我一样是用它的高性能做别的事情(画电脑图),那就没必要了。 它比较厚重(真的很沉,尤其是最新款的),而且硬件更换升级比较麻烦(比普通笔记本维护费高,毕竟你都买得起外星人了) -
lenovo 是个什么品牌?[2022-04-22]
1994年,联想在香港证券交易所成功上市;4年后,联想生产了自有品牌的第一百万台个人电脑。2003年,联想将其英文标识从“Legend”更换为“Lenovo”,其中“Le”取自原标识“Legend”,代表着秉承其一贯传统,新增加的“novo”取自拉丁词“新”,代表着联想的核心是创新精神。2004年,联想公司正式从“Legend”更名为“Lenovo”。