I'm working with JPA 2.0-EclipseLink-Hibernate 3 on MySQL 5. My classes structure is like below (I simplified it)
public class Status implements Serialization {
private String name = null;
public Status(string n) {
this.name = n;
}
@Override
public String toString() {
return this.name;
}
}
@Entity
@Table(name="ACCOUNT")
public class Account {
public static final Status ACTIVE = new Status("AC");
public static final Status DISABLE = new Status("DI");
private Status status = null;
private l开发者_StackOverflow中文版ong id = 0;
public Account(long id, Status s) {
this.id = id;
this.status = s;
}
@Id
@Column(name="id")
public long getId() {
return this.id;
}
public setId(long id) {
this.id = id;
}
@Column(name="status")
public Status getStatus() {
return this.status;
}
public setStatus(Status s) {
this.status = s;
}
}
Now, my question is, when I insert a new Account using EntityManager.persist, the program throws an exception:
java.sql.SQLException: Incorrect string value: '\xAC\xED\x00\x05sr...' for column 'status' at row 1
And after I added a new Account manually into the database, then I select it, the program say:
Exception Description: Could not deserialize object from byte array. Internal Exception: java.io.EOFException Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[status-->ACCOUNT.status] Descriptor: RelationalDescriptor(net.serenco.serenekids.model.bean.account.Account --> [DatabaseTable(ACCOUNT)])
Please tell me how to control what value will be written to DB, and by the way, please help me fix these errors. Thanks in advance!
---------------- EDIT ----------------
I have managed to find out the solution myself. It is:
@Embeddable
public class Status implements Serialization {
...
protected String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
}
@Entity
@Table(name="ACCOUNT")
public class Account {
...
@Embedded
@AttributeOverrides( {
@AttributeOverride(name="name", column = @Column(name="status") )
} )
public Status getStatus() {
return this.status;
}
...
}
精彩评论