Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » oneToMany with composite key using eclipselink(Mapping of oneToMany with aggregate key using eclipselink gives me ORA-01400)
oneToMany with composite key using eclipselink [message #1614535] Fri, 13 February 2015 10:01 Go to next message
Jan Andersen is currently offline Jan AndersenFriend
Messages: 3
Registered: January 2015
Junior Member
I am trying to map the classic bi-directional oneToMany using eclipselink. My problem is that when i want to insert a new 'child' i get SQLIntegrityConstraintViolationException.

The database is described like this :

@Entity
@IdClass(KuponPK.class)
@Table(name = "KUPON", schema = "POST", catalog = "")
public class Kupon implements Serializable {

  private Integer id;
  private String spil;
  private Collection<Kombination> kombinationList;

  @OneToMany(mappedBy = "kupon", cascade = CascadeType.PERSIST)
  public Collection<Kombination> getKombinationList() {
    return kombinationList;
  }

  public class KuponPK implements Serializable {

    private Integer id;
    private String spil;

    @Id
    @Column(name = "ID", nullable = false, insertable = true, updatable = true, precision = 0)
    public Integer getId() {
      return id;
    }

    @Id
    @Column(name = "SPIL", nullable = false, insertable = true, updatable = true, length = 5)
    public String getSpil() {
      return spil;
    }

    @Entity
    @Table(name = "KOMBINATION", schema = "POST", catalog = "")
    public class Kombination {
      private Integer id;
      private String sorteringOrden;
      private Integer sorteringNr;
      private Integer antalSpillede;
      private BigDecimal odds;
      private Kupon kupon;

      @ManyToOne
      @JoinColumns({@JoinColumn(name = "KUPON_ID", referencedColumnName = "ID", nullable = false, insertable=false, updatable=false),
              @JoinColumn(name = "KUPON_SPIL", referencedColumnName = "SPIL", nullable = false, insertable=false, updatable=false)})
      public Kupon getKupon() {
        return kupon;
      }


In my stateless session i have a Kupon object and i create a new Kombination where i set the Kupon and try to merge, but im getting Internal Exception: java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("KOMBINATION"."KUPON_ID") which is obvious since its part of primary key

I am setting the Kombination to Kupon and the Kupon to Kombination, but that doesnt make any difference

How can can set the composite foreign key of Kombination ??

[Updated on: Sat, 14 February 2015 14:56]

Report message to a moderator

Re: oneToMany with composite key using eclipselink [message #1626048 is a reply to message #1614535] Fri, 20 February 2015 20:05 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
How are you setting the "KOMBINATION"."KUPON_ID" field? Your mapping only shows the Kombination->Kupon relationship which has this field marked as insertable=false, updatable=false, so it has to be set through another mapping. With this configuration you need to be setting both the Kombination.kupon reference as well as that property with the kupon.id value.

You can also look into JPA 2.0 which uses derived Ids, allowing you to specify that your ManyToOne maps your KUPON_ID field. See http://www.eclipse.org/eclipselink/api/2.0.2/javax/persistence/MapsId.html and http://stackoverflow.com/a/4795755/496099 for examples
Re: oneToMany with composite key using eclipselink [message #1631443 is a reply to message #1626048] Mon, 23 February 2015 19:59 Go to previous messageGo to next message
Jan Andersen is currently offline Jan AndersenFriend
Messages: 3
Registered: January 2015
Junior Member
There is no kupon_id or kupon_spil in kombination. I believed that these 2 composite keys somehow should/could be set through the manyToOne relation (kupon) ?!

If i were to use mapsId i would believe that it should be having both kupon_spil and kupon_id as parameter but that doesnt seem possible.

In the StackOverflow example there is a joincolumn with one parameter - here i have 2 (joincolumnS) so somehow im missing something here.

I've tried to draw the 2 tables with only the primary key and foreign keys
if that should somehow help to clarify
  • Attachment: kombination.png
    (Size: 8.33KB, Downloaded 274 times)
  • Attachment: kupon.png
    (Size: 6.71KB, Downloaded 225 times)

[Updated on: Mon, 23 February 2015 20:22]

Report message to a moderator

Re: oneToMany with composite key using eclipselink [message #1633188 is a reply to message #1631443] Tue, 24 February 2015 18:35 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
If they are to be set through the relation, you should not be marking them as insertable=false, updatable=false - these settings are telling JPA not to update the fields, ever, regardless of what you do to the relationship.
Other then an extra join column being required, relationships to entities with composite PKs work exactly the same, so any examples are still valid. Your issue here is the insertable/updatable settings, but you should probably look at your pK class as well, as the annotations within the KuponPK class are ignored and should be on the respective Kupon properties instead.
Re: oneToMany with composite key using eclipselink [message #1637205 is a reply to message #1633188] Thu, 26 February 2015 15:49 Go to previous message
Jan Andersen is currently offline Jan AndersenFriend
Messages: 3
Registered: January 2015
Junior Member
I removed the annotations on the KuponPK and furthermore i removed the
insertable=false, updatable=false in Kombination on the joinColumn for both kupon_id and kupon_spil
but now im getting an error

16:41:08,179 ERROR [stderr] (default task-1) Exception [EclipseLink-48] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
16:41:08,179 ERROR [stderr] (default task-1) Exception Description: Multiple writable mappings exist for the field [KOMBINATION.KUPON_SPIL]. Only one may be defined as writable, all others must be specified read-only.
16:41:08,179 ERROR [stderr] (default task-1) Mapping: org.eclipse.persistence.mappings.ManyToOneMapping[kupon]
16:41:08,180 ERROR [stderr] (default task-1) Descriptor: RelationalDescriptor(dk.xxxxx.entities.post.Kombination --> [DatabaseTable(xxx.KOMBINATION)])
Previous Topic:EclipseLink bugs: where/how to report?
Next Topic:Doing all my transactions in one transaction
Goto Forum:
  


Current Time: Sat Nov 09 03:06:24 GMT 2024

Powered by FUDForum. Page generated in 0.03866 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top