知识点
相关文章
更多最近更新
更多hibernate中基于annotation(注解)的many2many双向
2019-02-28 23:40|来源: 领悟书生
修改hibernate中多对多映射关系中的例子,让它基于annotation(注解)的方式。
简单的多对多映射关系
Admin.java
package org.zttc.itat.model; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; @Entity @Table(name="t_admin") public class Admin { private int id; private String name; private Set<Role> roles; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToMany(mappedBy="admins") public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } }
Role.java
package org.zttc.itat.model; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; @Entity @Table(name="t_role") public class Role { private int id; private String name; private Set<Admin> admins; public Role() { admins = new HashSet<Admin>(); } public void add(Admin admin) { admins.add(admin); } @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToMany @JoinTable(name="t_role_admin",joinColumns={@JoinColumn(name="rid")}, inverseJoinColumns={@JoinColumn(name="aid")}) public Set<Admin> getAdmins() { return admins; } public void setAdmins(Set<Admin> admins) { this.admins = admins; } }
把多对多分为两个一对多的关系进行映射
Teacher.java
package org.zttc.itat.model; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; @Entity @Table(name="t_teacher") public class Teacher { private int id; private String name; private Set<TeacherCourse> tcs; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToMany(mappedBy="teacher") @LazyCollection(LazyCollectionOption.EXTRA) public Set<TeacherCourse> getTcs() { return tcs; } public void setTcs(Set<TeacherCourse> tcs) { this.tcs = tcs; } }
Course.java
package org.zttc.itat.model; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; @Entity @Table(name="t_course") public class Course { private int id; private String name; private Set<TeacherCourse> tcs; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToMany(mappedBy="course") @LazyCollection(LazyCollectionOption.EXTRA) public Set<TeacherCourse> getTcs() { return tcs; } public void setTcs(Set<TeacherCourse> tcs) { this.tcs = tcs; } }
TeacherCourse.java
package org.zttc.itat.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="t_teacher_course") public class TeacherCourse { private int id; private double ach; private Teacher teacher; private Course course; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public double getAch() { return ach; } public void setAch(double ach) { this.ach = ach; } @ManyToOne @JoinColumn(name="tid") public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } @ManyToOne @JoinColumn(name="cid") public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } }
测试代码请参考hibernate中多对多映射关系
本文链接:hibernate中基于annotation(注解)的many2many双向,领悟书生原创(笔记),转载请注明出处http://www.656463.com/article/406
相关问答
更多-
hibernate 4 扫描注解类[2023-12-06]
hibernate 用的注解都是基于JPA规范的,就是注解都在javax.persistence 这个包下,好像不用配置自动扫描 直接加注解就可以用 -
hibernate级联保存多对一怎么写?注解啊[2023-07-19]
尽量不要在程序里关联,直接在数据库建表的时候关联,这样你的代码就不用设置任何的关系,直接做自己的操作就行了,数据库建表有个外键关联,可以设置属性,你设置成Update和delete关联就行了,具体不会的话,我可以截操作图给你 -
默认保留策略是RetentionPolicy.CLASS ,这意味着默认情况下,注释信息在运行时不会保留: 编译器会将注释记录在类文件中,但不需要在运行时由VM保留。 这是默认行为。 相反,请使用RetentionPolicy.RUNTIME : 注释要由编译器记录在类文件中,并在运行时由VM保留,以便反射性地读取它们。 ...您使用@Retention元注释指定的@Retention : @Retention(RetentionPolicy.RUNTIME) public @interface NewAn ...
-
Hibernate配置很好。 问题在于DB中的表定义。 为了一切工作,应该自动生成的列需要使用默认值进行定义。 即数据库表创建后,需要执行以下命令: ALTER TABLE [dbo].[task] ADD CONSTRAINT [DF_task_created_date] DEFAULT (getdate()) FOR [created_date] GO 应该为任何应该自动生成的字段完成此操作。 这并不明显,在Hibernate文档(至少在5.1版本中)中没有提及,因此即使使用@ValueGener ...
-
我如何在没有注解的情况下使用Hibernate验证器?(How can I use a Hibernate validator without an annotation on a field?)[2024-02-27]
对的,这是可能的。 试试类似的, Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); Car car = new Car(null); Set> constraintViolations = validator.validateProperty(car, "manufacturer"); 看看这里 。 Yes, it is possible. T ... -
您可以为此字符串编写自己的UserType public class LowerCaseString implements UserType { //.... public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException { Hibernate. ...
-
你不能。 您需要编写一些代码来执行此操作(无论是在应用加载时间还是使用apt ) 我有相同的场景,并创建了我自己的注释: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface DependsOn { Class extends Annotation>[] value(); /** * Specifies whether all dependencie ...
-
尝试修改集而不是替换它。 即: quotation.getQuotationItem().clear(); quotation.getQuotationItem().addAll(newSet); 这是由于一些冬眠的怪异。 除此之外,还有一点关于命名。 您的Quotation.quotationItem应该命名为quotationItems因为它包含多个QuotationItem 。 Try to modify the set rather than replace it. i.e.: ...
-
因为@Formula仅支持标准SQL,所以我想出了使用数据库会话变量的解决方法。 @ManyToOne(fetch=FetchType.EAGER) @JoinColumnsOrFormulas({ @JoinColumnOrFormula(column=@JoinColumn(name = "par_mascode", referencedColumnName = "mascode", nullable = false, insertable = false, updatable = ...
-
没有必要,JPA遵循约定优于配置原则,这意味着总有一些默认值可以用注释覆盖。 在@JoinColumn的情况下,默认的列名是这样生成的:
_ field_name是您的案例中的address , id_column_name是指相关实体的id,即id 。 因此,默认情况下你会得到address_id 。 It is not necessary, JPA follows convention over configuration principle wh ...