在使用 JPA 注释 OneToOne/OneToMany 从 Cassandra 检索数据时,我们得到一个空对象(参见下面的示例,其中“item”为空)。
[
{
"idProduct":"095102f1-a987-4f7c-88c3-153d80b6977f",
"item":{
"idItem":"b75acb06-eab6-48c1-99e9-fc7d48cf930b",
"shortDescription":"Item 71",
"longDescription":"An another item 71",
"name":"Item Named 71",
"image":"Image 71"
},
"options":[
{
"idProductOption":"0fa701dc-5394-47ea-86f6-e3dbf6c263da",
"idProduct":"095102f1-a987-4f7c-88c3-153d80b6977f",
"productOptionValue":[
{
"idProductOptionValue":"1b594b56-7767-4909-9d16-add51903c0f2",
"idProductOption":"0fa701dc-5394-47ea-86f6-e3dbf6c263da",
"item":null
}
]
}
]
}
]
如果我们直接访问“ProductOptionValue”,它会完美加载。看到它正确加载(因此,我们认为这是加载的级别限制)。
[
{
"idProductOptionValue":"1b594b56-7767-4909-9d16-add51903c0f2",
"idProductOption":"0fa701dc-5394-47ea-86f6-e3dbf6c263da",
"item":{
"idItem":"17803826-0be6-4b94-813d-76abf969fa97",
"shortDescription":"Item 41",
"longDescription":"An item 41",
"name":"Item Named 41",
"image":"Image 41"
}
}
]
我们使用以下注释来关联对象。
@Entity
@Table(name = "Product", schema = "kunderaexamples@cassandra_pu")
public class Product {
@Id
@Column(name = "idProduct")
private String idProduct;
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
private List<ProductOption> productOption;
@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@JoinColumn(name = "idItem", table = "Item")
private Item item;
public String getIdProduct() {
return idProduct;
}
public void setIdProduct(String idProduct) {
this.idProduct = idProduct;
}
public List<ProductOption> getOptions() {
return productOption;
}
public void setOptions(List<ProductOption> options) {
this.productOption = options;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public Product() {
}
}
@Entity
@Table(name = "ProductOption", schema = "kunderaexamples@cassandra_pu")
public class ProductOption {
@Id
@Column(name = "idProductOption")
private String idProductOption;
@Column(name = "idProduct")
private String idProduct;
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
private List<ProductOptionValue> productOptionValue;
public String getIdProductOption() {
return idProductOption;
}
public void setIdProductOption(String idProductOption) {
this.idProductOption = idProductOption;
}
public List<ProductOptionValue> getProductOptionValue() {
return productOptionValue;
}
public void setProductOptionValue(List<ProductOptionValue> productOptionValues) {
this.productOptionValue = productOptionValues;
}
public String getIdProduct() {
return idProduct;
}
public void setIdProduct(String idProduct) {
this.idProduct = idProduct;
}
public ProductOption() {
}
}
@Entity
@Table(name = "ProductOptionValue", schema = "kunderaexamples@cassandra_pu")
public class ProductOptionValue {
@Id
@Column(name = "idProductOptionValue")
private String idProductOptionValue;
@Column(name = "idProductOption")
private String idProductOption;
@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@JoinColumn(name = "idItem", table = "Item")
private Item item;
public String getIdProductOptionValue() {
return idProductOptionValue;
}
public void setIdProductOptionValue(String idProductOptionValue) {
this.idProductOptionValue = idProductOptionValue;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public String getIdProductOption() {
return idProductOption;
}
public void setIdProductOption(String idProductOption) {
this.idProductOption = idProductOption;
}
public ProductOptionValue() {
}
}
@Entity
@Table(name = "Item", schema = "kunderaexamples@cassandra_pu")
public class Item {
@Id
@Column(name = "idItem")
private String idItem;
@Column(name = "short_description")
private String shortDescription;
@Column(name = "long_description")
private String longDescription;
@Column(name = "name")
private String name;
@Column(name = "image")
private String image;
// @Column(name = "context")
// private Context context;
//
// @Column(name = "inventory")
// private Inventory inventory;
//
// @Column(name = "price")
// private ItemPrice price;
/*
* GETTERS AND SETTERS
* */
public String getIdItem() {
return idItem;
}
public void setIdItem(String idItem) {
this.idItem = idItem;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getLongDescription() {
return longDescription;
}
public void setLongDescription(String longDescription) {
this.longDescription = longDescription;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
// public Context getContext() {
// return context;
// }
//
// public void setContext(Context context) {
// this.context = context;
// }
//
// public Inventory getInventory() {
// return inventory;
// }
//
// public void setInventory(Inventory inventory) {
// this.inventory = inventory;
// }
//
// public ItemPrice getPrice() {
// return price;
// }
//
// public void setPrice(ItemPrice price) {
// this.price = price;
// }
public Item() {
}
}