tenwit wrote:
I just meant that you can do "int myPrimitive = 3;". But you already knew that...
Hi there,
I'm trying to set up an index over 2 Strings and 1 Integer via the following code section:
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;
}
public 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();
}
}
As you can see there are default values set in the empty constructor and the empty constructor is called from each other contructor so any variable can't be uninitialized.
Even thought I should have set a default value for each of the index variables I get the following Exception thrown when inserting an object of this class into my database:
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
Does anyone have an idea of how to solve this issue as it should be somehow related to hibernate not "recognizing" the default value for
lastname.
Any help would be appreciated.
Thanks in advance,
Marc