• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with OneToOne mapping

 
hal arres
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I want to do a one-to-one mapping with hibernate. In the beginning it seems to be very easy, but now I am not so sure. I tried and tried, but I always failed.

Maybe someone of you could help me to fix my problem. I become more and more desprate...

To do this mapping I changed two existing tables to the following structure:


Now I want to map them into my Entities, which look like this:


and



When I trying to read this entities everything works fine, but as soon as I want to insert one I get the following exception:


This is the part where I am trying to insert the entity:


In the row of sql statements I can see, that the sequence is executed, but this is all.

I searched very long for a solution and tried different suggestions, but none worked.
So I want to ask you, if someone could help.
It would be very nice...

All the best,
arres
 
Oliver Tik
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hal,

i'm not sure, if I really understood your problem. Do you want to use the Document's ID/PK as the EventProfile's ID/PK?

Why then do you try to generate this ID instead of setting it programmatically? You could just drop all this generation annotations in Eventprofile

# public class Eventprofile {
#
# @Id
# @Column(name = "TEP_ID")
# private Long id;

and then

# Document document = new Document();
# document.setRemarks("My Hibernate-Unittest");
# em.persist(document);
# Eventprofile eventprofile = document.getEventprofile();
# eventprofile.setEventtypeText("My Eventtypetext.");
# eventprofile.setID(document.getID())
# document = em.merge(document);

Why wouldn't this work?

Cheers
Oliver

 
hal arres
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Oliver,

thank you for your answer.

Your right, I use the documents pk as the eventprofile PK.
The way to set it programatically does work, but if I do it this way, I have to save each entity on its own. But I want to safe both objects at once.
Or is it the standard-way to safe each object in a One-To-One relation on its own?

I found a way to do safe the relation at once by using the method "persist" instead of "merge".
But now I am a little bit confused, because Many-To-One or One-To-Many relations can be persist by using "merge".
Do you know why merge does not work for One-To-One?

Greetings,
Hal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic