- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
注意:我是 JPA 开发的新手,正在快速学习试用,所以请原谅我可能遗漏的任何明显内容。
以下是相关的 JPA 实体。地址未列出,但它是一个非常简单的实体,具有一个名为 id 的 @Id。
ShipTo.java
package model;
import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.sql.Timestamp;
/**
* The persistent class for the ship_to database table.
*
*/
@Entity
@Table(name="ship_to")
@NamedQuery(name="ShipTo.findAll", query="SELECT s FROM ShipTo s")
public class ShipTo implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ShipToPK id;
//bi-directional many-to-one association to Customer
@ManyToOne
@JoinColumns({
@JoinColumn(name="company_id", referencedColumnName="company_id"),
@JoinColumn(name="customer_id", referencedColumnName="customer_id")
})
private Customer customer;
//bi-directional one-to-one association to Address
@OneToOne
@JoinColumn(name="ship_to_id", referencedColumnName="id")
private Address address;
ShipToPK.java
package model;
import java.io.Serializable;
import javax.persistence.*;
/**
* The primary key class for the ship_to database table.
*
*/
@Embeddable
public class ShipToPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="company_id", insertable=false, updatable=false)
private String companyId;
@Column(name="ship_to_id")
private long shipToId;
public ShipToPK() {
}
public String getCompanyId() {
return this.companyId;
}
public void setCompanyId(String companyId) {
this.companyId = companyId;
}
public long getShipToId() {
return this.shipToId;
}
public void setShipToId(long shipToId) {
this.shipToId = shipToId;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ShipToPK)) {
return false;
}
ShipToPK castOther = (ShipToPK)other;
return
this.companyId.equals(castOther.companyId)
&& (this.shipToId == castOther.shipToId);
}
public int hashCode() {
final int prime = 31;
int hash = 17;
hash = hash * prime + this.companyId.hashCode();
hash = hash * prime + ((int) (this.shipToId ^ (this.shipToId >>> 32)));
return hash;
}
}
运行应用程序时出现以下错误:
Descriptor Exceptions:
---------------------------------------------------------
Exception [EclipseLink-48] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Multiple writable mappings exist for the field [ship_to.ship_to_id]. Only one may be defined as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[address]
Descriptor: RelationalDescriptor(model.ShipTo --> [DatabaseTable(ship_to)])
我的 ShipTo 实体需要与基于 ShipToPK.ship_to_id = Address.id 的 Address 建立一对一关系
同样,我的客户实体需要具有基于 CustomerPK.customerId = Address.id 的相同类型的关系
对此的任何帮助将不胜感激。谢谢,
最佳答案
您有两个使用 Ship_to.ship_to_id 字段的映射,这两个映射都是可写的。通常,要使其正常工作,您需要决定要使用哪个映射来控制和设置该字段,并将另一个映射标记为 insertable=false、updatable=false。
但在本例中,该字段是一个 ID 字段,也可在您可能希望通过关系设置的外键映射中使用。这是一种常见的特殊情况,JPA 2.0 引入了派生 ID 支持,允许使用 @ID 注释来标记关系。由于您使用的是 embeddableID,因此您还可以使用 @MapsId注释,以便 JPA 使用引用实体的值来设置 EmbeddedID 的 ShipToId 属性。
类似于:
//bi-directional one-to-one association to Address
@OneToOne
@MapsId("shipToId")
@JoinColumn(name="ship_to_id", referencedColumnName="id")
private Address address;
JoinColumn 定义字段,@MapsId 强制它覆盖 ShipToId 属性上的任何列定义。
关于java - JPA 将具有 PK 的实体映射到具有复合 PK 的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35163072/
我正在实现一个显示容器级别的图表。根据填充水平,线条的颜色应该改变(例如,接近最大值时应该显示红色)。我不想计算线条的不同部分并手动设置它们的颜色,而是想定义一个颜色自动改变的带。我想用自定义 Com
#include int main(void) { int days, hours, mins; float a, b, c, total, temp, tempA, tempB; a
if()//first if { if()//second if statement; } else statement; 我知道 else 与第一个 if 匹配,但我的问题是为什么?我是
以下代码中测试了 Ready 的哪个实例,为什么? interface type TObject1 = class ... public property Ready: boole
我刚刚花了相当多的时间来寻找像这个 plunk 中的差距.问题没那么简单。这是一个动态创建的页面,一些具有 margin-bottom 的组件恰好显示在 .main 的最后。 在我指责 CSS 之前,
我的程序应该在对话中创建圆形图标。我有三个按钮,每个按钮代表要制作的图标的颜色。因此,如果我点击不同的按钮 10 次,我的程序应该创建 10 个不同颜色的圆圈。这是我的代码,分为 2 个类: impo
我读过; A compound literal is a C99 feature that can be used to create an array with no name. Consider
当您创建一个复合 View 并为其扩充 xml 布局文件时,如下所示: public class CompundLayout extends LinearLayout{...} 这会像这样用根扩展一个
我正在创建一个带有标签和文本框的复合 uibinder 小部件。 预期用途是: The text to be put in the box. 我找到了如何使用自定义 @UiConstruc
任何人都可以举一个结合使用设计模式组合和责任链的实际例子吗? 谢谢 最佳答案 一个非常实际的例子是 GUI 设计,例如 Qt 框架。 QObject 可以是单个对象或多个对象的组合。 QObjects
我在这个项目中的一些表单中使用了复合 View 模型的模式。它工作得很好。 在这种情况下,我有一个 VendorAddress View 模型。我在这个项目的几个地方使用了 Address(es),所
我正在尝试构建一个我认为需要多个 JOIN 的 SQL 查询,但我不知道语法。 这是每个表(带有列名)的粗略示例。 T1( key ,名称) T2(键,fkeyT1) T3(键,fkeyT2) 我想从
我有一个 Composite我希望能够以编程方式启用/禁用。 Control.setEnabled(boolean enabled)方法工作正常,但它没有提供任何小部件被禁用的视觉信息。 我想做的是让
如果子域不是“mobile”并且文件名不是“design”或“photo”,我想回显某些内容,因此 echo if (not“mobile”且不是“design”)或(not“mobile”而不是“照
我有一张有几列的 table 。第 1 列和第 2 列可以包含四个 alpha 值中的任何一个:set={A,B,C,D}。 我想检查每列是否包含集合中的两个值之一。所以我想简化这个陈述: SELEC
我创建了一个全局数据类型,并在页面中使用表单渲染器让用户填写数据并提交到网站。 默认的英语工作正常。现在,当我尝试支持第二种语言时,我遇到了问题。根据复合文档: 1.在 ~/Frontend/Comp
我需要将自定义对象作为值存储在字典中,例如具有两个复合整数键的 datastrukturer。 (复合 ID) 我尝试使用数组作为键,但两者都不起作用,因为我猜这只是指向该数组的指针,用作键 如果我能
版本:3.2.1 关系 表B中的两列与表A中的两列相关联。 表A-> hasMany->表B 表B->属于--表A B.a_id = A.a_id B.a_name = A.a_name 食谱 在食谱
我创建了一个全局数据类型,并在页面中使用表单渲染器让用户填写数据并提交到网站。 默认的英语工作正常。现在,当我尝试支持第二种语言时,我遇到了问题。根据复合文档: 1.在 ~/Frontend/Comp
当前版本的 Log4net 是否有办法创建具有复合滚动样式的 RollingFileAppender,其中滚动文件始终保留给定的扩展名(在我的情况下为 .log)? 我想要的格式示例: MyLog.l
我是一名优秀的程序员,十分优秀!