This content has been marked as final.
Show 17 replies
-
15. Re: Help setting up entity bean with complex PK
dornus Sep 19, 2005 11:52 AM (in response to dornus)This does not work. Here is my exact code in case I am missing something.
package par; import javax.persistence.*; import java.io.Serializable; import java.sql.Timestamp; @Entity @Table(name = "example") public class Example implements Serializable { private ExamplePK examplePK; private Timestamp date; // This is the suggested workaround for foreign keys combined as a Primary Key @ManyToOne @JoinColumn(name="myFKeyOne", insertable=false, updatable=false) private User myFKeyOne; @ManyToOne @JoinColumn(name="myFKeyTwo", insertable=false, updatable=false) private User myFKeyTwo; public Example() { } @EmbeddedId public ExamplePK getExamplePK() { return examplePK; } public void setExamplePK( final ExamplePK examplePK ) { this.examplePK = examplePK; } public Timestamp getDateMyFKeyTwo() { return dateMyFKeyTwo; } public void setDate( final Timestamp date ) { this.date = date; } // If I uncomment these I get an Exception /* public User getMyFKeyOne() { return myFKeyOne; } public void setMyFKeyOne( final User myFKeyOne ) { this.myFKeyOne = myFKeyOne; } public User getMyFKeyTwo() { return myFKeyTwo; } public void setMyFKeyTwo( final User myFKeyTwo ) { this.myFKeyTwo = myFKeyTwo; } */ }
package par; import java.io.Serializable; public class ExamplePK implements Serializable { public int myFKeyOne; public int myFKeyTwo; public ExamplePK() { } public int getMyFKeyOne() { return myFKeyOne; } public void setMyFKeyOne( final int myFKeyOne ) { this.myFKeyOne = myFKeyOne; } public int getMyFKeyTwo() { return myFKeyTwo; } public void setMyFKeyTwo( final int myFKeyTwo ) { this.myFKeyTwo = myFKeyTwo; } public int hashCode() { return myFKeyOne + myFKeyTwo; } public boolean equals( final Object obj ) { if ( obj == null ) { return false; } else if ( obj == this ) { return true; } else if ( !( obj instanceof ExamplePK ) ) { return false; } final ExamplePK pk = (ExamplePK) obj; return pk.myFKeyOne == myFKeyOne && pk.myFKeyTwo == myFKeyTwo; } }
and the exception is:null; CausedByException is: could not execute query. 2005-09-19 11:42:14,998 DEBUG [org.hibernate.hql.ast.ErrorCounter] throwQueryException() : no errors 2005-09-19 11:42:14,998 DEBUG [org.hibernate.hql.ast.QueryTranslatorImpl] HQL: select count(o) from par.Example o 2005-09-19 11:42:14,998 DEBUG [org.hibernate.hql.ast.QueryTranslatorImpl] SQL: select count((example0_.myFKeyOne, example0_.myFKeyTwo)) as col_0_0_ from example example0_ 2005-09-19 11:42:14,998 DEBUG [org.hibernate.hql.ast.ErrorCounter] throwQueryException() : no errors 2005-09-19 11:42:14,998 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 2005-09-19 11:42:14,998 DEBUG [org.hibernate.jdbc.ConnectionManager] opening JDBC connection 2005-09-19 11:42:14,998 DEBUG [org.hibernate.jdbc.JDBCContext] successfully registered Synchronization 2005-09-19 11:42:14,998 DEBUG [org.hibernate.SQL] select count((example0_.myFKeyOne, example0_.myFKeyTwo)) as col_0_0_ from example example0_ 2005-09-19 11:42:14,998 DEBUG [org.hibernate.jdbc.AbstractBatcher] preparing statement 2005-09-19 11:42:14,998 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 2005-09-19 11:42:14,998 DEBUG [org.hibernate.jdbc.AbstractBatcher] closing statement 2005-09-19 11:42:14,998 DEBUG [org.hibernate.jdbc.ConnectionManager] aggressively releasing JDBC connection 2005-09-19 11:42:14,998 DEBUG [org.hibernate.jdbc.ConnectionManager] closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] 2005-09-19 11:42:14,998 DEBUG [org.hibernate.util.JDBCExceptionReporter] could not execute query [select count((example0_.myFKeyOne, example0_.myFKeyTwo)) as col_0_0_ from example example0_] java.sql.SQLException: Operand should contain 1 column(s) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1570) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665) at com.mysql.jdbc.Connection.execSQL(Connection.java:2978) at com.mysql.jdbc.Connection.execSQL(Connection.java:2902) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:930) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1024) at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:211) at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:137) at org.hibernate.loader.Loader.getResultSet(Loader.java:1676) at org.hibernate.loader.Loader.doQuery(Loader.java:662) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223) at org.hibernate.loader.Loader.doList(Loader.java:2147) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026) at org.hibernate.loader.Loader.list(Loader.java:2021) at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369) at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:296) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:992) at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74) at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:650) at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:43) at ejb3.UserTestBean.countExamples(UserTestBean.java:19) 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:585) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:99) at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:32) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88) at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:66) at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88) at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:61) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88) at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88) at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88) at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:32) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88) at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:91) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88) at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:148) at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:60) at $Proxy111.countExamples(Unknown Source) at web.test.TestServlet.doStuff(TestServlet.java:51) at web.test.TestServlet.doGet(TestServlet.java:32) at javax.servlet.http.HttpServlet.service(HttpServlet.java:697) at javax.servlet.http.HttpServlet.service(HttpServlet.java:810) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39) at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:157) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:407) at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112) at java.lang.Thread.run(Thread.java:595) 2005-09-19 11:42:15,008 WARN [org.hibernate.util.JDBCExceptionReporter] SQL Error: 1241, SQLState: 21000 2005-09-19 11:42:15,008 ERROR [org.hibernate.util.JDBCExceptionReporter] Operand should contain 1 column(s)
-
16. Re: Help setting up entity bean with complex PK
epbernard Sep 19, 2005 1:23 PM (in response to dornus)Do you have the ability to test on another DB?
-
17. Re: Help setting up entity bean with complex PK
dornus Sep 19, 2005 1:28 PM (in response to dornus)"epbernard" wrote:
Do you have the ability to test on another DB?
Unfortunately not.
I have MySql 5.0.11
I also installed the EJB 3.0 RC2 today, so I have all the latest from that.