-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Bidirectional one-to-many composite key - one side owner
PostPosted: Tue Nov 10, 2015 5:12 pm 
Newbie

Joined: Tue Nov 10, 2015 4:58 pm
Posts: 1
I am trying to create a bidirectional one-to-many association between two entities where the many side has a compound key. And one of the keys of the many side is coming from the one side. Also, I need to have the many side the owner of the association. Below is an example code showing what my code looks like.

Child Class which is the many side. Its primary key is "name" and "parent". "parent" is coming from the association.
Code:
public class child{

    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name="parent", column=@Column(name="PARENT", nullable=false)),
            @AttributeOverride(name="name", column=@Column(name="NAME", nullable=false))})
    private ChildId id;

    @MapsId("pool")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="PARENT", nullable = false, updatable = false, insertable = false)
    private Parent parent;
}


ChildId is the Embedded id.

Code:
@Embeddable
public class childId{

    @Column(name = "PARENT")
    private String parent;

    @Column(name = "NAME")
    private String name;
}


Parent Class which is the one side. I need to have this side owner of the association.
Code:
public class parent{

    @Id
    @Column(name = "NAME")
    private String name;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name="id", nullable = false)
    private Set<Child> childs;


Question1:
When I run this code, I got the following exception:
Code:
    Hibernate: update CHILD set id=? where NAME=? and PARENT=?
    2015-11-10 15:36:16,960 66657 [qtp797818094-25] DEBUG o.s.j.s.SQLErrorCodeSQLExceptionTranslator - Translating SQLException with SQL state '42703', error code '-206', message [DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=ID, DRIVER=3.61.86]; SQL was [] for task [DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=ID, DRIVER=3.61.86]
    2015-11-10 15:36:16,972 66669 [qtp797818094-25] DEBUG o.h.e.jdbc.spi.SqlExceptionHelper - could not execute statement [n/a]
    msjava.dbpool.DBPoolSQLException: DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=ID, DRIVER=3.61.86 (DataSource: dataSource, Type: DB2)

As it shows, it is trying to update the child and not insert! how can I solve it?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.