在复合键中使用 JoinColumns 时的 TypeDescriptor 错误
Posted
技术标签:
【中文标题】在复合键中使用 JoinColumns 时的 TypeDescriptor 错误【英文标题】:Wrong TypeDescriptor when using JoinColumns in a composite Key 【发布时间】:2015-10-19 19:59:30 【问题描述】:我一直在摸索,试图弄清楚以下映射有什么问题。我了解以下映射对于 ORM 并不理想,但数据库就是这样,我无法更改其结构。我正在使用 JPA 2.1 和 Hibernate 5.0.2.Final。
@MappedSuperclass
public abstract class BaseEntity<T extends Serializable> implements Serializable
protected T id;
@Id
public T getId()
return id;
protected void setId(T id)
this.id = id;
@Table(name = "campaign")
@AttributeOverride(name = "id", column = @Column(name = "campaign_id") )
public class Campaign extends BaseEntity<String>
// attributes, getters and setters
@Embeddable
public class CampaignBroadcastPK implements Serializable
@ManyToOne
@JoinColumn(name = "campaign_id", insertable = false, updatable = false)
private Campaign campaign;
@Column(name = "broadcast_date")
private LocalDate broadcastDate;
// getters and setters
@Entity
@Table(name = "campaign_broadcast")
public class CampaignBroadcast implements Serializable
@EmbeddedId
private CampaignBroadcastPK id;
// attributes, getters and setters
@Embeddable
public class CampaignBroadcastProcessPK implements Serializable
@ManyToOne
@JoinColumns(
@JoinColumn(name = "campaign_id", insertable = false, updatable = false),
@JoinColumn(name = "broadcast_date", insertable = false, updatable = false)
)
private CampaignBroadcast broadcast;
@Column(name = "process_date)
private LocalDate processDate;
// getters and setters
@Entity
@Table(name = "campaign_broadcast_process")
public class CampaignBroadcastProcess implements Serializable
@EmbeddedId
private CampaignBroadcastProcessPK id;
// attributes, getters and setters
除了这个结构,我还有一个转换器来处理LocalDate到java.sql.Date
,它被autoApply=true
注解。
当我尝试从entityManager.find()
加载CampaignBroadcastProcess
时,休眠尝试将campaign_id
转换为日期,即使它被映射为字符串,导致java.sql.Date
抛出IllegalStateException
,因为字符串传递给valueOf
不是有效的日期字符串。
我怀疑 Hibernate 混淆了 CampaignBroadcastProcessPK
上的 JoinColumns 类型,将它们都作为 LocalDate 处理。
有人遇到过这个问题吗?
【问题讨论】:
【参考方案1】:我不完全确定为什么,但是改变两个@JoinColum
的顺序解决了这个问题。现在的映射如下:
@ManyToOne
@JoinColumns(
@JoinColumn(name = "broadcast_date", insertable = false, updatable = false),
@JoinColumn(name = "campaign_id", insertable = false, updatable = false)
)
private CampaignBroadcast broadcast;
【讨论】:
这就是我所说的——错误的隐式映射。考虑使用显式绑定而不是依赖来自注释的隐式顺序 不,事实并非如此,因为即使使用 referencedColumnName 映射也不起作用。【参考方案2】:尝试将明确的 referencedColumnName 添加到您的 @JoinColumn
。您描述的错误可能是由于复合键列的隐式映射顺序错误
@ManyToOne
@JoinColumns(
@JoinColumn(name = "campaign_id", referencedColumnName = "campaign_id", ...),
@JoinColumn(name = "broadcast_date", referencedColumnName = "broadcast_date", ...) )
private CampaignBroadcast broadcast;
【讨论】:
【参考方案3】:我认为这是因为 hibernate 选择了第一个数据类型,它是数据类型并将其映射到 Join 列
【讨论】:
以上是关于在复合键中使用 JoinColumns 时的 TypeDescriptor 错误的主要内容,如果未能解决你的问题,请参考以下文章