Ich habe genau das gleiche Problem und habe nach deinem Hinweis folgenden Code benutzt, um das Ganze zu lösen:
Klasse Person:
Code:
@Entity
@Embeddable
public class Person {
private Long id;
private int age;
private String firstname;
private String lastname;
@GeneratedValue
public Long getId() {
return id;
}
@SuppressWarnings("unused")
private void setId(Long id) {
this.id = id;
}
@Id
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Column(length=30)
@Id
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@Id
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
private Person() {
firstname = "foo";
lastname = "bar";
age = 0;
}
public Person(String firstName, String lastName, int age) {
this();
this.firstname = firstName;
this.lastname = lastName;
this.age = age;
}
@Override
public boolean equals(Object object) {
if (object instanceof Person) {
Person otherPerson = (Person) object;
return
firstname.equals(otherPerson.getFirstname()) &&
lastname.equals(otherPerson.getLastname()) &&
(age == otherPerson.getAge());
}
return false;
}
@Override
public int hashCode() {
return (firstname + lastname + age).hashCode();
}
}
Ich habe also eine ID über
firstName,
lastName und
age generiert. Alelrdings bekomme ich beim Einfügen folgende Exception
Code:
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at events.EventManager.main(EventManager.java:40)
Caused by: java.sql.BatchUpdateException: Field 'lastname' doesn't have a default value
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:665)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 8 more
Gibt es in Hibernate einen Weg, dieses Problem zu umgehen bzw. einen Default-Wert für eine Spalte anzugeben.
Wie im Konstruktor zu sehen habe ich die Variable
lastName initialisiert, was jedoch den oben genannten Fehler nicht behebt.
Danke für euere Hilfe.