Hibernate不插入子对象

时间:2016-10-17 16:58:59

标签: java hibernate hibernate-mapping

我有以下实体

@Entity
@Table(name = "school")
public class School {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "sch_id", unique = true, nullable = false)
    private Integer schoolId;

    ... 
}

@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "stu_id", unique = true, nullable = false)
    private Integer studentId;

    ...

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "school_id", referencedColumnName = "sch_id", insertable = false, updatable = false, nullable = false)
    @JsonIgnore
    private School school;
}

现在,当我保存新的学生对象时,生成的学生表插入查询不包含字段'school_id'。

SessionFactory.getCurrentSession().saveOrUpdate(student), 

如何在插入查询中包含“school_id”。请注意我不想插入/更新学校对象。我只是将schoolId设置在学校对象中,并将其设置为学生学校对象。

0 个答案:

没有答案