我目前在Spring环境中使用Hibernate,但我遇到了一些问题。
事实上,我有3个实体:
我正在努力的是创建第四个表,其中包含其他三个外键,如Crit_Grav_Occ(idC,idG,idO)
Criticite
@Entity
@Table(name = "criticite")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "criticite")
public class Criticite implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "criticite")
private Integer criticite;
@ManyToMany
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JoinTable(name = "criticite_gravite_occurrence",
joinColumns = @JoinColumn(name="criticites_id", referencedColumnName="id"),
inverseJoinColumns = @JoinColumn(name="occurrences_id", referencedColumnName="id"))
private Set<Occurrence> occurrences = new HashSet<>();
@ManyToMany
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JoinTable(name = "criticite_gravite_occurrence",
joinColumns = @JoinColumn(name="criticites_id", referencedColumnName="id"),
inverseJoinColumns = @JoinColumn(name="gravites_id", referencedColumnName="id"))
private Set<Gravite> gravites = new HashSet<>();
Gravite
@Entity
@Table(name = "gravite")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "gravite")
public class Gravite implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "description")
private String description;
@ManyToMany(mappedBy = "gravites", cascade = {CascadeType.ALL})
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Criticite> criticites = new HashSet<>();
发生
@Entity
@Table(name = "occurrence")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "occurrence")
public class Occurrence implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "description")
private String description;
@ManyToMany(mappedBy = "occurrences", cascade = {CascadeType.ALL})
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Criticite> criticites = new HashSet<>();
现在......当我想使用CrudRepository中的save保存批评时,我得到了
引起:java.sql.SQLException:字段'occurrences_id'没有 默认值
上面奇怪的是:
Hibernate:插入critite_gravite_occurrence(critites_id, gravites_id)值(?,?)
虽然我应该期待这个
Hibernate:插入critite_gravite_occurrence(critites_id, gravites_id,occurences_id)值(?,?,?)
欢迎任何帮助:)