JPA多对多插入

时间:2015-08-01 08:26:33

标签: java hibernate jpa many-to-many

我有3张桌子。 1是父母,1是学生,1是student_parent。这是父母和学生之间的多对多关系。 parentID来自父表,studentID来自student表。 student_parent表具有studentID,parentID和childRelationship。我需要知道如何使用JPA(hibernate)更新childRelationship字段。

请注意,我使用Java Spring和JPA / Hibernate来更新我的关系数据库,还有其他几个表,但我想知道的只是更新childRelationship字段。

@Entity
@Table(name = "Student")
@Proxy(lazy = false)
public class Student{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "studentID", nullable = false)
private int studentID;


@Column(name="firstName")
private String firstName;   

@Column(name="middleName")
private String middleName;

@Column(name="lastName")
private String lastName;    

@Column(name="validationID")
private String validationID;

@Column(name="birthDate")
private Date birthDate;

@Column(name="gender")
private char gender;

@Column(name="ethnicity")
private String ethnicity;

@Column(name="photo") // character type has to confirmed
private byte[] photo;

@Column(name="gradeLevel")
private int gradeLevel;

@Column(name="rewardPoints")
private String rewardPoints;

@Column(name="dateCreated")
private Date dateCreated;

@Column(name="schoolID")
private Integer schoolID;

   @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "addressID")
    private Address address;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "userName")
    private UserAccount userAccount;

    @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    @JoinTable(name="student_parent",
            joinColumns = {@JoinColumn(name = "studentID")},
            inverseJoinColumns = {@JoinColumn(name = "parentID")})
    private Set<Parent> parents = new HashSet<Parent>();


    @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    @JoinTable(name="student_community",
            joinColumns = {@JoinColumn(name = "studentID")},
            inverseJoinColumns = {@JoinColumn(name = "communityID")})
    private Set<Community> communities = new HashSet<Community>();



    //getter and setting

然后是父母。

@Entity
@Proxy(lazy = false)
public class Parent {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int parentID;   

@Column(name="userName")
private String userName;

@Column(name="firstName")
private String firstName;   

@Column(name="middleName")
private String middleName;

@Column(name="lastName")
private String lastName;    

@Column(name="validationID")
private String validationID;

@Column(name="maritalStatus")
private String maritalStatus;

@Column(name="noOfChildren")
private int noOfChildren;

@Column(name="birthDate")
private Date birthDate;

@Column(name="gender")
private char gender;

@Column(name="ethnicity")
private String ethnicity;

@Column(name="annualIncome")
private Float annualIncome;

@Column(name="dateCreated")
private Date dateCreated;

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "addressID")
    private Address address;

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "userName",unique = true, nullable = false, insertable=false, updatable=false)
private UserAccount userAccount;

//getter and setter

1 个答案:

答案 0 :(得分:0)

您已有一个关联表,您需要将其作为关联类引入实体世界。关联类的主键是由学生ID和父ID组成的复合键。别忘了在主键类上实现hashcode和equals。通过此映射,您可以访问m:n关系的属性(即&#34; childRelationship&#34;列)。您还需要添加getter和setter,或者进行字段访问。

var personSchema = Schema({
  _id     : Number,
  name    : String,
  age     : Number,
  stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

var storySchema = Schema({
  _creator : { type: Number, ref: 'Person' },
  title    : String,
  fans     : [{ type: Number, ref: 'Person' }]
});