Hello,
ich tried to establish a 1:1 relation between 2 tables but it doesn't work.
Above is the code, I get the following exception:
Quote:
ids for this class must be manually assigned before calling save(): at.eventtiming.domain.Contact; nested exception is
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():
at.eventtiming.domain.Contact
Code:
public final class DaoMain {
public static void main(String[] args) {
final ApplicationContext ctx = new FileSystemXmlApplicationContext("src/service.xml");
final HibernateParticipantDao dao = (HibernateParticipantDao) ctx.getBean("participantDao");
final Participant participant = new Participant();
participant.setFirstname("Hans");
participant.setSurname("Maier");
//participant.setChipnumber("DF-12345");
final Contact contact = new Contact();
contact.setEmail("hans.mair@web.de");
contact.setTelnumber("0664/534508697");
participant.setContact(contact);
dao.saveParticipant(participant);
final List<Participant> participantList = dao.getAllParticipants();
System.out.println("ParticipantList: " + participantList);
}
}
Code:
@Entity
@SuppressWarnings("serial")
@Table(name="TParticipant")
public class Participant implements Serializable {
private Integer pk_part;
private String firstname;
private String surname;
private String chipnumber;
private Contact contact;
public Participant() {}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getPk_part() {
return pk_part;
}
public void setPk_part(final Integer pk_part) {
this.pk_part = pk_part;
}
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public Contact getContact() {
return contact;
}
public void setContact(final Contact contact) {
this.contact = contact;
}
...
Code:
@Entity
@SuppressWarnings("serial")
@Table(name="TContact")
public class Contact implements Serializable {
@Id
private Integer pk_contact;
private String email;
private String telnumber;
private String fax;
public Contact() {}
public Integer getPk_contact() {
return pk_contact;
}
public void setPk_contact(final Integer pk_contact) {
this.pk_contact = pk_contact;
}