|
I have a simple entity with a composite primary key:
@Entity
@Table(name = "person_conversation_meta")
@IdClass(ConversationMeta.Id.class)
public class ConversationMeta {
public static class Id implements Serializable {
private int conversation;
private int person;
public Id() {
}
public Id(int conversation, int person) {
this.conversation = conversation;
this.person = person;
}
public int getConversation() {
return conversation;
}
public void setConversation(int conversation) {
this.conversation = conversation;
}
public int getPerson() {
return person;
}
public void setPerson(int person) {
this.person = person;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Id id = (Id)o;
if (conversation != id.conversation) return false;
if (person != id.person) return false;
return true;
}
@Override
public int hashCode() {
int result = conversation;
result = 31 * result + person;
return result;
}
}
@javax.persistence.Id
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "person_id", nullable = false)
private Person person;
@javax.persistence.Id
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "conversation_id", nullable = false)
private Conversation conversation;
@Column(name = "is_read", nullable = false)
private boolean read;
@Column(nullable = false)
private boolean starred;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Conversation getConversation() {
return conversation;
}
public void setConversation(Conversation conversation) {
this.conversation = conversation;
}
public boolean isRead() {
return read;
}
public void setRead(boolean read) {
this.read = read;
}
public boolean isStarred() {
return starred;
}
public void setStarred(boolean starred) {
this.starred = starred;
}
}
Now I want to create a query to get all ConversationMeta for a particular Conversation:
CriteriaQuery<ConversationMeta> criteriaQuery = criteriaBuilder.createQuery(ConversationMeta.class);
Root<ConversationMeta> conversationMeta = criteriaQuery.from(ConversationMeta.class);
criteriaQuery.where(criteriaBuilder.equal(conversationMeta.get(ConversationMeta_.conversation), conversation));
The criteriaBuilder.equal(conversationMeta.get(ConversationMeta_.conversation), conversation) fails with IllegalArgumentException: Unaware how to convert value. The ConversationMeta_ metamodel class appears to be generating correctly, listing the conversation attribute as SingularAttribute<ConversationMeta, Conversation> which is why I'm checking for equality with a Conversation and not an int. But the attribute in reality has a javaType of int, which is why the Conversation entity is being converted.
Changing the ConversationMeta.Id class to contain Conversation and Person entities, rather than {{int}}s is a fine workaround. I just wanted to record this odd behavior.
|