problem with embedded object [message #1436713] |
Fri, 03 October 2014 09:43 |
|
Hi.
I've entity definition which is shown below. Now I try following SQL query:
select o_ from Author_4_5 o_ where o_.key.firstname = :firstname and o_.key.lastname = :lastname
During building the query I got following exception:
java.lang.RuntimeException: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Problem compiling [select o_ from test.schulung.jpa.entities.Author_4_5 o_ where o_.key.firstname = :firstname and o_.key.lastname = :lastname].
[62, 78] The state field path 'o_.key.firstname' cannot be resolved to a valid type.
[96, 111] The state field path 'o_.key.lastname' cannot be resolved to a valid type.
Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Problem compiling [select o_ from test.schulung.jpa.entities.Author_4_5 o_ where o_.key.firstname = :firstname and o_.key.lastname = :lastname].
[62, 78] The state field path 'o_.key.firstname' cannot be resolved to a valid type.
[96, 111] The state field path 'o_.key.lastname' cannot be resolved to a valid type.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
Any ideas what goes wrong?
Thanks
Rainer
package test.schulung.jpa.entities;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.apache.commons.lang3.StringUtils;
import rh.jpa.entities.BaseEntity;
@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames = { "lastname", "firstname" }))
public class Author_4_5 extends BaseEntity {
private Integer id;
private Key key;
private String text;
private Author_4_5() {
this("", "", "");
}
public Author_4_5(final String lastname, final String firstname, final String text) {
super();
this.setKey(new Key(lastname, firstname));
this.setText(text);
}
public Author_4_5(final String lastname, final String firstname) {
this(lastname, firstname, ""); //$NON-NLS-1$
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public final Integer getId() {
return this.id;
}
public final void setId(final Integer id) {
this.id = id;
}
@Embedded
public final Key getKey() {
return this.key;
}
public final void setKey(final Key key) {
this.key = key;
}
@Basic
public final String getText() {
return this.text;
}
public final void setText(final String text) {
this.text = text;
}
@Override
public final int hashCode() {
final int prime = 31;
int result = 1;
// CHECKSTYLE:OFF
result = prime * result + this.getKey().hashCode();
result = prime * result + ((this.text == null) ? 0 : this.text.hashCode());
// CHECKSTYLE:ON
return result;
}
@Override
public final boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Author_4_5)) {
return false;
}
final Author_4_5 other = (Author_4_5) obj;
return this.getKey().equals(other.getKey())
&& StringUtils.equals(this.getText(), other.getText());
}
@Override
public final String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Author [key="); //$NON-NLS-1$
builder.append(this.getKey());
builder.append(", text="); //$NON-NLS-1$
builder.append(this.getText());
builder.append(", "); //$NON-NLS-1$
builder.append(super.toString());
builder.append("]"); //$NON-NLS-1$
return builder.toString();
}
public static class Key implements Serializable {
private String lastname;
private String firstname;
Key() {
this("", "");
}
public Key(final String lastname, final String firstname) {
super();
this.setLastname(lastname);
this.setFirstname(firstname);
}
public final String getLastname() {
return this.lastname;
}
public final void setLastname(final String lastname) {
this.lastname = lastname;
}
public final String getFirstname() {
return this.firstname;
}
public final void setFirstname(final String firstname) {
this.firstname = firstname;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Key [lastname=");
builder.append(this.lastname);
builder.append(", firstname=");
builder.append(this.firstname);
builder.append("]");
return builder.toString();
}
@Override
public boolean equals(final Object object) {
if (!(object instanceof Key)) {
return false;
}
final Key key = (Key) object;
return this.getLastname().equals(key.getLastname())
&& this.getFirstname().equals(key.getFirstname());
}
@Override
public int hashCode() {
return this.getLastname().hashCode() + this.getFirstname().hashCode();
}
}
}
package rh.jpa.entities;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.apache.commons.lang3.time.FastDateFormat;
@MappedSuperclass
@Access(AccessType.PROPERTY)
@EntityListeners(rh.jpa.utils.Stamper.class)
public class BaseEntity implements Serializable private String editor;
private Calendar lastUpdate;
public BaseEntity() {
super();
final Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(0);
this.setEditor(""); //$NON-NLS-1$
this.setLastUpdate(calendar);
}
@Basic
@Column(name = "EDITOR")
public final String getEditor() {
return this.editor;
}
public final void setEditor(final String editor) {
this.editor = editor;
}
@Basic
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "LAST_UPDATE")
public final Calendar getLastUpdate() {
return this.lastUpdate;
}
public final void setLastUpdate(final Calendar lastUpdate) {
this.lastUpdate = lastUpdate;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
final FastDateFormat dtf = FastDateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
final String pattern = dtf.getPattern().replace("ss", "ss.SSS");
builder.append("BaseEntity [user=");
builder.append(this.getEditor());
builder.append(", lastUpdate=");builder.append(FastDateFormat.getInstance(pattern).format(this.getLastUpdate()));
builder.append("]");
return builder.toString();
}
}
[Updated on: Sat, 04 October 2014 19:34] Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02004 seconds