hibernate annotation one-to-many
2012-10-14 18:53
357 查看
hibernate一对多,多的一方建立外键,例:
CREATE TABLE `person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `personName` varchar(45) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `house` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `houseNo` varchar(45) NOT NULL, `person_id` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FK42AD7008BA56E9D` (`person_id`), CONSTRAINT `FK42AD7008BA56E9D` FOREIGN KEY (`person_id`) REFERENCES `person` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
import java.io.Serializable; 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.OneToMany; import javax.persistence.Table; @Entity @Table public class Person implements Serializable{ /** * */ private static final long serialVersionUID = -6396702070077903027L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public Long getId() {return id;} public void setId(Long id) {this.id = id;} private Long id; @Column(length=45,nullable=false) public String getPersonName() {return personName;} public void setPersonName(String personName) {this.personName = personName;} private String personName; @OneToMany(mappedBy="person",cascade=CascadeType.ALL) public Set<House> getHouseSet() {return houseSet;} public void setHouseSet(Set<House> houseSet) {this.houseSet = houseSet;} private Set<House> houseSet; }
import java.io.Serializable; 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.Table; @Entity @Table public class House implements Serializable{ /** * */ private static final long serialVersionUID = -2630598805462422037L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public Long getId() {return id;} public void setId(Long id) {this.id = id;} private Long id; @Column(length=45,nullable=false) public String getHouseNo() {return houseNo;} public void setHouseNo(String houseNo) {this.houseNo = houseNo;} private String houseNo; @ManyToOne(cascade=CascadeType.ALL) @JoinColumn public Person getPerson() {return person;} public void setPerson(Person person) {this.person = person;} private Person person; }
相关文章推荐
- hibernate manyToOne lazy to one Annotation
- hibernate3.5和spring结合使用annotation时onetomany的bug
- Hibernate 使用 Annotation 之 OneToMany(单向关联关系)
- Hibernate关系映射(四)一对多单向关联@OneToMany Annotation方式
- org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.demo.pojo.IdCard
- org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity() defined: com.bjsxt.model.Student.courses
- Hibernate 使用 Annotation 之 OneToMany(双向关联关系)
- hibernate OneToMany (Annotation)
- hibernate OneToMany (Annotation)
- org.hibernate.AnnotationException: Referenced property not a (One|Many)ToOne: in mappedBy of
- Hibernate – One-To-Many Example (Annotation)
- [Hibernate] - Annotations - One To Many
- hibernate的many-to-one的级联删除问题
- Hibernate ORM One to Many
- Hibernate的onetomany和manytoone联合使用(十八)
- hibernate映射many-to-one 属性
- hibernate 的many to one 的问题IllegalArgumentException occurred while calling setter of hibernatePOJO
- Hibernate报错:could not initialize proxy -no session以及 one-to-many配置
- hibernate many-to-one
- Hibernate – One-to-One example (Annotation)