# @ManyToMany Sequence angeben möglich



## y0dA (4. Aug 2010)

Hi!

Ich habe folgende n:m Beziehung:

Person <--1:n-- Titelzuordnung (Jointabelle) --n:1--> Titel (sind Stammdaten und werden nicht angelegt sondern sind schon vorhanden)

Person:
ID PK
etc.

Titelzuordnung:
ID PK
Person-ID
Titel-ID

Titel:
ID PK
etc.

Weiters erzeugen wir unsere PK mit Sequencen, sprich jede Tabelle hat eine eigene Sequence (auch die JoinTable).

Nun habe ich folgendes probiert, in der Klasse Person:

```
@ManyToMany(cascade = CascadeType.REFRESH)
	@JoinTable(name = "titelzuordnung", joinColumns = { @JoinColumn(name = "person") }, inverseJoinColumns = { @JoinColumn(name = "titel") })
	private List<Titel> titelListe;
```

Jedoch wenn ich nun eine Person speichern möchte bekomme folgende Exception:

```
Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: DB2 SQL error: SQLCODE: -407, SQLSTATE: 23502, SQLERRMC: ID {prepstmnt 16974208 INSERT INTO TWRDB.titelzuordnung (person, titel) VALUES (?, ?) [params=(int) 10, (int) 3]} [code=-407, state=23502]
	at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:192)
	at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access$700(LoggingConnectionDecorator.java:57)
	at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.executeUpdate(LoggingConnectionDecorator.java:866)
	at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:269)
	at org.apache.openjpa.jdbc.kernel.JDBCStoreManager$CancelPreparedStatement.executeUpdate(JDBCStoreManager.java:1590)
	at org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.executeUpdate(PreparedStatementManagerImpl.java:151)
	at org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushAndUpdate(PreparedStatementManagerImpl.java:120)
	... 68 more

2010-08-04 10:46:18,015 WARN  [GeronimoConnectionEventListener] connectionErrorOccurred called with null
com.ibm.db2.jcc.a.SqlException: DB2 SQL error: SQLCODE: -407, SQLSTATE: 23502, SQLERRMC: ID
	at com.ibm.db2.jcc.a.rf.e(rf.java:1680)
	at com.ibm.db2.jcc.a.rf.a(rf.java:1239)
	at com.ibm.db2.jcc.a.rf.a(rf.java:1225)
	at com.ibm.db2.jcc.b.jb.h(jb.java:132)
	at com.ibm.db2.jcc.b.jb.a(jb.java:43)
	at com.ibm.db2.jcc.b.w.a(w.java:30)
	at com.ibm.db2.jcc.b.cc.g(cc.java:160)
	at com.ibm.db2.jcc.a.rf.n(rf.java:1219)
	at com.ibm.db2.jcc.a.sf.gb(sf.java:1790)
	at com.ibm.db2.jcc.a.sf.d(sf.java:2266)
	at com.ibm.db2.jcc.a.sf.Y(sf.java:540)
	at com.ibm.db2.jcc.a.sf.executeUpdate(sf.java:523)
	at org.tranql.connector.jdbc.PreparedStatementHandle.executeUpdate(PreparedStatementHandle.java:103)
	at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:269)
	at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.executeUpdate(LoggingConnectionDecorator.java:864)
	at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:269)
	at org.apache.openjpa.jdbc.kernel.JDBCStoreManager$CancelPreparedStatement.executeUpdate(JDBCStoreManager.java:1590)
	at org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.executeUpdate(PreparedStatementManagerImpl.java:151)
	at org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushAndUpdate(PreparedStatementManagerImpl.java:120)
	at org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushInternal(PreparedStatementManagerImpl.java:89)
	at org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flush(PreparedStatementManagerImpl.java:72)
	at org.apache.openjpa.jdbc.kernel.OperationOrderUpdateManager.flush(OperationOrderUpdateManager.java:218)
	at org.apache.openjpa.jdbc.kernel.OperationOrderUpdateManager.flush(OperationOrderUpdateManager.java:102)
	at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:89)
	at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:72)
	at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.flush(JDBCStoreManager.java:721)
	at org.apache.openjpa.kernel.DelegatingStoreManager.flush(DelegatingStoreManager.java:130)
	at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:2029)
	at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1927)
	at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1845)
	at org.apache.geronimo.transaction.manager.TransactionImpl.beforeCompletion(TransactionImpl.java:522)
	at org.apache.geronimo.transaction.manager.TransactionImpl.beforeCompletion(TransactionImpl.java:507)
	at org.apache.geronimo.transaction.manager.TransactionImpl.beforePrepare(TransactionImpl.java:400)
	at org.apache.geronimo.transaction.manager.TransactionImpl.commit(TransactionImpl.java:260)
	at org.apache.geronimo.transaction.manager.TransactionManagerImpl.commit(TransactionManagerImpl.java:250)
	at org.apache.openejb.core.transaction.TransactionPolicy.commitTransaction(TransactionPolicy.java:138)
	at org.apache.openejb.core.transaction.TxRequired.afterInvoke(TxRequired.java:76)
	at org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:233)
	at org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:188)
	at org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:165)
	at org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(EjbObjectProxyHandler.java:217)
	at org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(EjbObjectProxyHandler.java:77)
	at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:321)
	at org.apache.openejb.util.proxy.Jdk13InvocationHandler.invoke(Jdk13InvocationHandler.java:49)
	at $Proxy59.savePerson(Unknown Source)
	at at.gv.brz.zwr.web.suche.SucheController.personenSuche(SucheController.java:126)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
	at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
	at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
	at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:78)
	at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:57)
	at javax.faces.component.UICommand.broadcast(UICommand.java:127)
	at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:329)
	at org.ajax4jsf.component.AjaxViewRoot.broadcastEventsForPhase(AjaxViewRoot.java:304)
	at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:261)
	at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:474)
	at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
	at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:103)
	at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:76)
	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:178)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
	at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
	at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
	at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.geronimo.tomcat.valve.DefaultSubjectValve.invoke(DefaultSubjectValve.java:56)
	at org.apache.geronimo.tomcat.GeronimoStandardContext$SystemMethodValve.invoke(GeronimoStandardContext.java:406)
	at org.apache.geronimo.tomcat.valve.GeronimoBeforeAfterValve.invoke(GeronimoBeforeAfterValve.java:47)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:555)
	at org.apache.geronimo.tomcat.valve.ThreadCleanerValve.invoke(ThreadCleanerValve.java:40)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Thread.java:619)
```

Kann ich bei @ManyToMany eventuell noch irgendwo die Sequence mitgeben, damit JPA weiß wie die ID für Titelzuordnung erstellt wird?


----------



## Final_Striker (4. Aug 2010)

Wozu braucht titelzuordnung eine extra id? In der Regel wird da doch der zusammengesetzte Schlüssel aus Person und Titel verwendent.


----------



## y0dA (4. Aug 2010)

Final_Striker hat gesagt.:


> Wozu braucht titelzuordnung eine extra id? In der Regel wird da doch der zusammengesetzte Schlüssel aus Person und Titel verwendent.



Das ist doch jetzt irrelevant oder nicht..


----------



## Final_Striker (4. Aug 2010)

Ich weiß nicht ob das irrelevant ist oder nicht, aber meines Wissens nach, generiert man in den Zwischentabellen keine eigenen Primärschlüssel, sondern verwendet einen zusammengesetzten Primärschlüssel aus den zwei Fremdschlüsseln der beiden anderen Tablen. 

Person:
ID PK
...

Titelzuordnung:
Person-ID PK
Titel-ID PK

Titel:
ID PK
...


----------

