I've been banging my head around, trying to figure what's wrong with the following mapping. I understand the following mapping is not ideal for ORM, but that's how the database is and I cannot change its structure. I'm using JPA 2.1 and 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
}
Besides this structure, I also have a converter to handle LocalDate to java.sql.Date
, which is annotated with autoApply=true
.
When I try to load CampaignBroadcastProcess
through entityManager.find()
, hibernate tries to convert campaign_id
to Date, even though it's mapped as a String, causing java.sql.Date
to throw an IllegalStateException
, because the String passed to valueOf
isn't a valid date String.
I'm suspicious Hibernate is mixing up the types of the JoinColumns on CampaignBroadcastProcessPK
, handling them both as a LocalDate.
Has someone ever faced this issue?
I'm not exactly sure why, but changing the order of the two @JoinColum
solved the issue. The mapping now is as below:
@ManyToOne
@JoinColumns({
@JoinColumn(name = "broadcast_date", insertable = false, updatable = false),
@JoinColumn(name = "campaign_id", insertable = false, updatable = false)
})
private CampaignBroadcast broadcast;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With