|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
Specifying a sequence as the pk generator for a many-to-many-join table relationshipWe have a many-to-many relationship between Receipt and CashBatch.
We have a cash_batch_receipt join table with: - id NUMERIC (primary key) - cash_batch_id NUMERIC (foreign key to cash_batch) - receipt_id NUMERIC (foreign key to cash_batch) and a SeqCashBatchReceiptJoin for generating pk values. How can we map this so that the sequence is used to generate the ID pk value? The following did not work. [In JpaCashBatch...] @ManyToMany(targetEntity=JpaReceipt.class, mappedBy="cashBatchList", fetch=FetchType.LAZY) @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SeqCashBatchReceiptJoin") @JoinTable ( name="cash_batch_receipt_join", joinColumns=@JoinColumn(name="cash_batch_id", referencedColumnName="id"), inverseJoinColumns=@JoinColumn(name="receipt_id", referencedColumnName="id") ) [In JpaReceipt...] @ManyToMany(targetEntity=JpaCashBatch.class, fetch=FetchType.LAZY) @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SeqCashBatchReceiptJoin") @JoinTable ( name="cash_batch_receipt_join", joinColumns=@JoinColumn(name="receipt_id", referencedColumnName="id"), inverseJoinColumns=@JoinColumn(name="cash_batch_id", referencedColumnName="id") ) private List<CashBatchEntity> cashBatchList = null; We get: java.sql.SQLException: ORA-01400: cannot insert NULL into ("CIS"."CASH_BATCH_RECEIPT_JOIN"."ID") Error Code: 1400 Call: INSERT INTO cash_batch_receipt_join (cash_batch_id, receipt_id) VALUES (?, ?) bind => [1002, 1191] Query: DataModifyQuery(sql="INSERT INTO cash_batch_receipt_join (cash_batch_id, receipt_id) VALUES (?, ?)") Thanks! I couldn't find any information on many-to-many JPA mappings for tables with independent primary keys. Regards, Mike _______________________________________________ eclipselink-users mailing list eclipselink-users@... https://dev.eclipse.org/mailman/listinfo/eclipselink-users |
|
|
Re: Specifying a sequence as the pk generator for a many-to-many-join table relationshipYou cannot put a @GeneratedValue on a @ManyToMany.
Generally if you have additional fields in a join table you need to create an object that maps to the join table to map the fields. See, http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns You can also use a database trigger to auto generate the value, or use custom SQL in your EclipseLink ManyToManyMapping (setInsertSQLString()) from a DescriptorCustomizer.
James Sutherland EclipseLink, TopLink Wiki: EclipseLink, TopLink Forums: TopLink, EclipseLink Book: Java Persistence |
|
|
Re: Specifying a sequence as the pk generator for a many-to-many-join table relationshipThanks.
I had already read Mapping_a_Join_Table_with_Additional_Columns. As a workaround, I had made the join into a separate entity with many-to-one relationships. However, I was hoping that another option was available. On Thu, Sep 10, 2009 at 10:10 AM, James Sutherland<jamesssss@...> wrote: > > You cannot put a @GeneratedValue on a @ManyToMany. > > Generally if you have additional fields in a join table you need to create > an object that maps to the join table to map the fields. > See, > http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns > > You can also use a database trigger to auto generate the value, or use > custom SQL in your EclipseLink ManyToManyMapping (setInsertSQLString()) from > a DescriptorCustomizer. > > > > Mike Kienenberger wrote: >> >> We have a many-to-many relationship between Receipt and CashBatch. >> >> We have a cash_batch_receipt join table with: >> - id NUMERIC (primary key) >> - cash_batch_id NUMERIC (foreign key to cash_batch) >> - receipt_id NUMERIC (foreign key to cash_batch) >> >> and a SeqCashBatchReceiptJoin for generating pk values. >> >> How can we map this so that the sequence is used to generate the ID pk >> value? >> >> The following did not work. >> >> >> [In JpaCashBatch...] >> >> @ManyToMany(targetEntity=JpaReceipt.class, >> mappedBy="cashBatchList", fetch=FetchType.LAZY) >> @GeneratedValue(strategy=GenerationType.SEQUENCE, >> generator="SeqCashBatchReceiptJoin") >> @JoinTable ( >> name="cash_batch_receipt_join", >> joinColumns=@JoinColumn(name="cash_batch_id", >> referencedColumnName="id"), >> inverseJoinColumns=@JoinColumn(name="receipt_id", >> referencedColumnName="id") >> ) >> >> >> [In JpaReceipt...] >> >> @ManyToMany(targetEntity=JpaCashBatch.class, fetch=FetchType.LAZY) >> @GeneratedValue(strategy=GenerationType.SEQUENCE, >> generator="SeqCashBatchReceiptJoin") >> @JoinTable ( >> name="cash_batch_receipt_join", >> joinColumns=@JoinColumn(name="receipt_id", >> referencedColumnName="id"), >> inverseJoinColumns=@JoinColumn(name="cash_batch_id", >> referencedColumnName="id") >> ) >> private List<CashBatchEntity> cashBatchList = null; >> >> >> We get: >> >> java.sql.SQLException: ORA-01400: cannot insert NULL into >> ("CIS"."CASH_BATCH_RECEIPT_JOIN"."ID") >> >> Error Code: 1400 Call: INSERT INTO cash_batch_receipt_join >> (cash_batch_id, receipt_id) VALUES (?, ?) bind => [1002, 1191] Query: >> DataModifyQuery(sql="INSERT INTO cash_batch_receipt_join >> (cash_batch_id, receipt_id) VALUES (?, ?)") >> >> Thanks! I couldn't find any information on many-to-many JPA mappings >> for tables with independent primary keys. >> >> Regards, >> Mike >> > > > ----- > http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland > http://www.eclipse.org/eclipselink/ > EclipseLink , http://www.oracle.com/technology/products/ias/toplink/ > TopLink > Wiki: http://wiki.eclipse.org/EclipseLink EclipseLink , > http://wiki.oracle.com/page/TopLink TopLink > Forums: http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , > http://www.nabble.com/EclipseLink-f26430.html EclipseLink > Book: http://en.wikibooks.org/wiki/Java_Persistence Java Persistence > -- > View this message in context: http://www.nabble.com/Specifying-a-sequence-as-the-pk-generator-for-a-many-to-many-join-table-relationship-tp25286091p25384004.html > Sent from the EclipseLink - Users mailing list archive at Nabble.com. > > _______________________________________________ > eclipselink-users mailing list > eclipselink-users@... > https://dev.eclipse.org/mailman/listinfo/eclipselink-users > eclipselink-users mailing list eclipselink-users@... https://dev.eclipse.org/mailman/listinfo/eclipselink-users |
| Free embeddable forum powered by Nabble | Forum Help |