Spring JPA - 多对一,没有完整的异物 [英] Spring JPA - Many to one without full foreign object
问题描述
我在 ManyToOne >这个回购。实体之间的关系是一个 Cart
(购物车)可以有许多 Item
s(在本教程)。
<
@Entity
@Table(name =CART)
公共类Cart
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =cart_id)
私人长ID;
@OneToMany(mappedBy =cart)
private Set< Item>项目;
/ *其余班级* /
strong>
@Entity
@Table(name =ITEMS)
public class Item
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =id)
私人长ID;
@Column(name =item_name)
private String itemName;
@ManyToOne
@JoinColumn(name =cart_id,nullable = false)
私人购物车; //需要一个整个Cart实例:(
/ *其余的类* /
问题
如何将项目
映射到购物车 code>类,而不会使
Item
类的整个 Cart
对象部分。
我想看看 Item
类看起来像这样
@Entity
@Table(name =ITEMS)
public class Item
{
@Id
@GeneratedValue(strategy = GenerationType .IDENTITY)
@Column(name =id)
私有长ID;
@Column(name =item_name)
private String itemName;
@ManyToOne
@JoinColumn(name =cart_id,nullable = false)
私人长卡车ID; //只是购物车的ID,而不是整个购物车对象
这给出错误
造成者:org.hibernate.AnnotationException:com.exa上的@OneToOne或@ManyToOne mple.demo.Item.cartId引用一个未知的实体:long
在org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:97)〜[hibernate-core-5.2.12.Final.jar:5.2。 12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processEndOfQueue(InFlightMetadataCollectorImpl.java:1752)〜[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
在org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1696)〜[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.boot .internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1583)〜[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.boot.model.process.spi。 MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)〜[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
这并不令我感到惊讶。但我想以某种方式能够在没有整个 Cart
实例的情况下实例化 Item 。理想情况下,
Cart
ID作为 Item
中的字段就足够了。
编辑
因此,如果我必须使项目
中如何使用 data-h2.sql
脚本初始化数据库?有没有SQL方式来表达 Cart
是!
您可以将引用完全移回到 Cart
中,并通过删除 mappedBy =cart code> @OneToMany
注释中的code>部分。
如果您仍然希望 cart_id
,您可以在 Cart
中使用java代码来设置它。
当然,所有这些都引出了一个问题:为什么你想要这种非JPA-ish的行为。如果您创建另一个问题,说明您的原始问题/关注,似乎有可能需要了解JPA。
I am experimenting with spring and jpa's ManyToOne
in this repo. The relationship between the entities is that one Cart
(shopping cart) can have many Item
s (following this tutorial).
Cart
@Entity
@Table(name="CART")
public class Cart
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cart_id")
private long id;
@OneToMany(mappedBy = "cart")
private Set<Item> items;
/* rest of class*/
Item
@Entity
@Table(name="ITEMS")
public class Item
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name="item_name")
private String itemName;
@ManyToOne
@JoinColumn(name = "cart_id", nullable = false)
private Cart cart; // needs an entire Cart instance :(
/* rest of class*/
Question
How can I map the Item
s to the Cart
class without making the entire Cart
object part of the Item
class.
I want to look the Item
class to look like this
@Entity
@Table(name="ITEMS")
public class Item
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name="item_name")
private String itemName;
@ManyToOne
@JoinColumn(name = "cart_id", nullable = false)
private long cartId; // just the ID of Cart, not the entire Cart object
This gives the errors
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.example.demo.Item.cartId references an unknown entity: long
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:97) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processEndOfQueue(InFlightMetadataCollectorImpl.java:1752) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1696) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1583) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
which doesn't really surprise me. But I want to be somehow able to instantiate an Item
without an entire Cart
instance. Ideally, just the Cart
ID as a field in Item
would suffice.
EDIT
So if I have to make Cart
a field in Item
how to I initialize the db with a data-h2.sql
script? There is no SQL way to express what a Cart
is!
You can just remove the reference back to the Cart
completely and make the complete relationship go just in one direction by dropping the mappedBy="cart"
part from the @OneToMany
annotation.
If you still want the cart_id
in the item you can set it using java code on the Cart
.
Of course, all this begs the question: Why do you want this behavior which is very un-JPA-ish. If you create another question stating your original question/concern it seems likely that there is something to learn about JPA.
这篇关于Spring JPA - 多对一,没有完整的异物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!