Hibernate(Spring JPA):@ManyToOne JoinColumns(这些是EmbeddedId)为null。

huangapple go评论60阅读模式
英文:

Hibernate (Spring JPA): @ManyToOne JoinColumns (which are an EmbeddedId) are null

问题

我没有看到我的错误,在stackoverflow和Google上进行了研究后,我认为代码应该是正确的。但是Hibernate(spring-boot-starter-data-jpa 2.2.4)仍然将JoinColumns填充为null

以下是我的OneToMany类:

@Entity
@Table(name = "tablea", schema = "")
public class TableAEntity implements Serializable {

	private static final long serialVersionUID = 7890327260188587351L;

	@EmbeddedId
	private MyId id;
	
	@OneToMany(cascade = ALL, mappedBy = "tableA", orphanRemoval = true, fetch = FetchType.LAZY)
	private List<TableBEntity> tableBentries;
	
	// Getter + Setter
}

我的EmbeddedId类:

@Embeddable
public class MyId implements Serializable {

	private static final long serialVersionUID = -8267953052238233498L;

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

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

	@Column(name = "idint")
	private BigInteger idint;
	
	// Getter + Setter
}

最后是我的ManyToOne类:

@Entity
@Table(name = "tableB", schema = "")
public class TableBEntity implements Serializable {

	private static final long serialVersionUID = -4648090658471459969L;

	@Id
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({
		@JoinColumn(name = "id", referencedColumnName = "id"),
		@JoinColumn(name = "iddate", referencedColumnName = "iddate"),
		@JoinColumn(name = "idint", referencedColumnName = "idint")
	})
	private TableAEntity tableA;
	
	// Some other attributes
	
	// Getter + Setter
}

正如您所看到的,我希望在两个表中使用属性(ididdateidint)作为组合主键。

当我创建TableAEntity对象时,我将多个TableBEntity对象添加到tableBentries属性中。对于每个TableBEntity,我都设置了对TableAEntity的引用(属性tableA)。当然,MyId(属性id)对象也被填充。

在保存TableAEntity对象后,Hibernate还会存储所有的TableBEntity,但是字段ididdateidint(所有的JoinColumn)都是null

有什么想法吗?

英文:

I didn't see my error and after a research on stackoverflow and Google I think the code should be correct. But Hibernate (spring-boot-starter-data-jpa 2.2.4) still fill the JoinColumns with null.

Here is my OneToMany class:

@Entity
@Table(name = &quot;tablea&quot;, schema = &quot;&quot;)
public class TableAEntity implements Serializable {

	private static final long serialVersionUID = 7890327260188587351L;

	@EmbeddedId
	private MyId id;
	
	@OneToMany(cascade = ALL, mappedBy = &quot;tableA&quot;, orphanRemoval = true, fetch = FetchType.LAZY)
	private List&lt;TableBEntity&gt; tableBentries;
	
	// Getter + Setter
}

My EmbeddedId class:

@Embeddable
public class MyId implements Serializable {

	private static final long serialVersionUID = -8267953052238233498L;

	@Column(name = &quot;id&quot;)
	private String id;

	@Column(name = &quot;iddate&quot;)
	private Date iddate;

	@Column(name = &quot;idint&quot;)
	private BigInteger idint;
	
	// Getter + Setter
}

And finally my ManyToOne class:

@Entity
@Table(name = &quot;tableB&quot;, schema = &quot;&quot;)
public class TableBEntity implements Serializable {

	private static final long serialVersionUID = -4648090658471459969L;

	@Id
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({
		@JoinColumn(name = &quot;id&quot;, referencedColumnName = &quot;id&quot;),
		@JoinColumn(name = &quot;iddate&quot;, referencedColumnName = &quot;iddate&quot;),
		@JoinColumn(name = &quot;idint&quot;, referencedColumnName = &quot;idint&quot;)
	})
	private TableAEntity tableA;
	
	// Some other attributes
	
	// Getter + Setter
}

As you may see I want to use the attributes (id, iddate and idint) as a combined primaray key in both tables.

When I create the TableAEntity object I add several TableBEntity objects to the tableBentries attribute. And for every TableBEntity I set the reference to the TableAEntity (attribute tableA). And of course the MyId (attribute id) object is filled.

After saveing the TableAEntity object, Hibernate also stores all TableBEntity but the fields id, iddate and idint (all JoinColumn's) are null.

Any idea?

答案1

得分: 1

似乎是我在 TableBEntity 中的 @Id 导致了这些问题。如果我将它移到另一个属性,它就正常工作。

英文:

It seems that @Id in my TableBEntity causes the problems. If I moved it to another attribute, it works.

huangapple
  • 本文由 发表于 2020年9月3日 14:45:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63718220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定