Re: entities not detected properly in JUnit Test case (OpenJPA 1.2.2)
Hi, thanks for this pretty straight answer. But one more thing that bothers me. Why is this only an issue while testing? When my app is running, i am pretty sure that i dont use the DP_PLZ_DA class somewhere in the code before the worfklow with the query arrives. So why does it work in normal runtime mode? --- regards Marc Logemann http://www.logemann.org http://www.logentis.de Am 06.09.2010 um 23:26 schrieb Rick Curtis: So why does OpenJPA fail on registering Entities? The problem is that when you don't list your entities, OpenJPA doesn't know that the alias 'DP_PLZ_DA' is an Entity until the x.y.z.DP_PLZ_DA class is loaded. If you were to look at an enhanced Entity you would see that OpenJPA adds a static initializer that will register each Entity with OpenJPA. An alternative to calling new DP_PLZ_DA(); would be to add something like this to each of your testcases. // List each Entity that this test is going to use. private Class[] _entities = new Class[]{x.y.z.DP_PLZ_DA.class,x.y.z.DP_PLZ_DB.class,etc... }; Thanks, Rick On Mon, Sep 6, 2010 at 4:14 PM, Marc Logemann l...@logemann.org wrote: Hi, i am using a pretty straightforward Spring/OpenJPA JUnit Testcase scenario: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = /appContext.xml) public class DHLLeitcodeCreatorTest { ... @Test public void getDHLLeitcode() { someBean.superDuperAction(); } } I am not explicitely declaring my entities in persistence.xml. Its all autodetected via Annotations. (at least during normal runtime). Inside superDuperAction(), a lot of queries are fired against OpenJPA. It fails on the first Query saying that it cant find Entity DP_PLZ_DA (weird class name, i know). Error message: The name DP_PLZ_DA is not a recognized entity or Now when i do this: @Test public void getDHLLeitcode() { DP_PLZ_DA df = new DP_PLZ_DA(); someBean.superDuperAction(); } It works and it can find the Entity. But why in gods name do i need to instantiate the class by myself in order to get detected? During normal runtime, this is no problem. Kevin Sutter reported something similar here ( http://www.ibm.com/developerworks/forums/thread.jspa?messageID=14459507) but i thought i can solve this by upgrading to 1.2.2 but i was mistaken. So why does OpenJPA fail on registering Entities? --- regards Marc Logemann http://www.logemann.org http://www.logentis.de
Re: entities not detected properly in JUnit Test case (OpenJPA 1.2.2)
Marc - I'm willing to bet that your Entities are being loaded before you execute the query. You could try setting *-verbose:class* on your app server jvm and add some debug to your app in the first place where you execute the query. These two pieces of information should help you get a better feeling as to what is going on. Now your next question... where is DP_PLZ_DA.class is being loaded? You could add a static initializer to DP_PLZ_DA.class and dump the stack trace to figure out who is loading it. Thanks, Rick On Tue, Sep 7, 2010 at 1:38 AM, Marc Logemann l...@logemann.org wrote: Hi, thanks for this pretty straight answer. But one more thing that bothers me. Why is this only an issue while testing? When my app is running, i am pretty sure that i dont use the DP_PLZ_DA class somewhere in the code before the worfklow with the query arrives. So why does it work in normal runtime mode? --- regards Marc Logemann http://www.logemann.org http://www.logentis.de Am 06.09.2010 um 23:26 schrieb Rick Curtis: So why does OpenJPA fail on registering Entities? The problem is that when you don't list your entities, OpenJPA doesn't know that the alias 'DP_PLZ_DA' is an Entity until the x.y.z.DP_PLZ_DA class is loaded. If you were to look at an enhanced Entity you would see that OpenJPA adds a static initializer that will register each Entity with OpenJPA. An alternative to calling new DP_PLZ_DA(); would be to add something like this to each of your testcases. // List each Entity that this test is going to use. private Class[] _entities = new Class[]{x.y.z.DP_PLZ_DA.class,x.y.z.DP_PLZ_DB.class,etc... }; Thanks, Rick On Mon, Sep 6, 2010 at 4:14 PM, Marc Logemann l...@logemann.org wrote: Hi, i am using a pretty straightforward Spring/OpenJPA JUnit Testcase scenario: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = /appContext.xml) public class DHLLeitcodeCreatorTest { ... @Test public void getDHLLeitcode() { someBean.superDuperAction(); } } I am not explicitely declaring my entities in persistence.xml. Its all autodetected via Annotations. (at least during normal runtime). Inside superDuperAction(), a lot of queries are fired against OpenJPA. It fails on the first Query saying that it cant find Entity DP_PLZ_DA (weird class name, i know). Error message: The name DP_PLZ_DA is not a recognized entity or Now when i do this: @Test public void getDHLLeitcode() { DP_PLZ_DA df = new DP_PLZ_DA(); someBean.superDuperAction(); } It works and it can find the Entity. But why in gods name do i need to instantiate the class by myself in order to get detected? During normal runtime, this is no problem. Kevin Sutter reported something similar here ( http://www.ibm.com/developerworks/forums/thread.jspa?messageID=14459507 ) but i thought i can solve this by upgrading to 1.2.2 but i was mistaken. So why does OpenJPA fail on registering Entities? --- regards Marc Logemann http://www.logemann.org http://www.logentis.de
problem using subclass object as query parameter
Hello everyone I have a problem using inheritance in OpenJPA. Here is my code @Entity @Table(name = BASEBRN) @Inheritance(strategy=InheritanceType.JOINED) @DiscriminatorColumn(name=TYPE,discriminatorType=DiscriminatorType.INTEGER) public class BaseBranch { @Id @Column(name = BASEID) private Long id; @Column(name=TYPE) @Enumerated(value = EnumType.ORDINAL) private BaseBranchType baseBranchType; public BaseBranch(Long id) { this.id = id; } public BaseBranch() { super(); } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public BaseBranchType getBaseBranchType() { return baseBranchType; } public void setBaseBranchType(BaseBranchType baseBranchType) { this.baseBranchType = baseBranchType; } @Override public boolean equals(Object object) { if (object != null !(object instanceof BaseBranch)) return false; BaseBranch other = (BaseBranch) object; if (this.id != null other.id != null (this.id.longValue() == other.id.longValue())) return true; return false; } @Override public int hashCode() { int hashCode = 0; if (id != null) hashCode += id.hashCode(); return hashCode; } } @Table(name = BRANCH) @DiscriminatorValue(1) @PrimaryKeyJoinColumn(name=BRANCHID, referencedColumnName=BASEID) public class Branch extends BaseBranch { @Column(name = CODE) private String code; public Branch() { } public Branch(Long id) { this.setId(id); } public Branch(String code) { this.code = code; } public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Branch other = (Branch) obj; if ((this.getId() == null) ? (other.getId() != null) : !this.getId().equals(other.getId())) { return false; } return true; } public int hashCode() { int hash = 7; hash = 23 * hash + (this.getId() != null ? this.getId().hashCode() : 0); return hash; } } After runing this code block Query q = entityManager.createNamedQuery(select b from Branch b where b=:branch); q.setParameter(branch, new Branch(1L)); b=q.getResultList(); - result query is something like this SELECT t1.BASEID, t1.TYPE, t0.CODE FROM BRANCH t0, BASEBRN t1 WHERE (1 1) AND t1.TYPE = ? AND t0.BRANCHID = t1.BASEID [params=(int) 1] OpenJPA never takes the parameter and always raises NoResultException. It seems for openJPA both following two params are the same . new Branch(1L) and new Branch() What's wrong with my code or is this maybe an openJPA bug? Any idea or help is appreciated thanks -- View this message in context: http://openjpa.208410.n2.nabble.com/problem-using-subclass-object-as-query-parameter-tp5506089p5506089.html Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: problem using subclass object as query parameter
Hi, The where clause in the query is obviously wrong : WHERE (1 1) // always returns false We've seen similar statements with embedded objects on earlier versions of OpenJPA (1.2.1 comes to mind). Which version of OpenJPA are you using, and are you doing build time or runtime enhancement? If you have specified any properties in persistence.xml please post them and it'll help us reproduce. Thanks -mike -- View this message in context: http://openjpa.208410.n2.nabble.com/problem-using-subclass-object-as-query-parameter-tp5506089p5507158.html Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Confusing warning about openjpa.jdbc.SynchronizeMappings
Hi Kevin, I can't offer a fix (yet), but I think I've pinpointed the problem. Here is a little test case: import static org.junit.Assert.assertEquals; import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl; import org.junit.Test; public class ConfigurationTest { @Test public void cloneTest() { JDBCConfigurationImpl config = new JDBCConfigurationImpl(); JDBCConfigurationImpl newConfig = (JDBCConfigurationImpl) config.clone(); assertEquals(config, newConfig); } } The warning message about the configuration property openjpa.jdbc.SynchronizeMappings is logged during config.clone(). clone() is implemented by invoking first toProperties(true) and then fromProperties() on the resulting properties map. fromProperties() removes all known properties from the map and then issues warnings for all remaining ones - at least that seems to be the intention. The problem is that SynchronizeMappings has no default value, or rather a default value of null. Thus, the map generated by toProperties(true) contains the key openjpa.jdbc.SynchronizeMappings with a value of null. fromProperties() in fact only removes the known properties with a non-null value, so the key openjpa.jdbc.SynchronizeMappings remains in the map and causes the warning. The warning would not appear if clone() invoked toProperties(false), but I'm not sure if this has any side effects. Moreover, in my test case, the assertion assertEquals(config, newConfig); fails - now the Javadoc of Object.clone() states that x.clone().equals(x) is not an absolute requirement (without giving any good reasons), but in this case I would definitely expect the clone of a configuration to be equal to the original. The assertion failure is caused by LockTimeout:0[int] != LockTimeout:0[int], so it appears that for x of class IntValue the invariant x.clone().equals(x) does not hold either (why?). Incidentally, the comment of ConfigurationImpl.toProperties() is outdated. It references a non-existing parameter getAll and a non-existing method getAllProperties(). Hopefully, this is enough input for someone familiar with the implementation details to come up with a fix... Best regards, Harald -- View this message in context: http://openjpa.208410.n2.nabble.com/Confusing-warning-about-openjpa-jdbc-SynchronizeMappings-tp5495438p5507719.html Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Confusing warning about openjpa.jdbc.SynchronizeMappings
Very good input, Harald. Thanks for your efforts. I'll post this information to the existing JIRA and we'll see if we can somebody to take a look. It would be great to get this one off the books. Kevin On Tue, Sep 7, 2010 at 1:44 PM, Harald Wellmann harald.wellm...@multi-m.dewrote: Hi Kevin, I can't offer a fix (yet), but I think I've pinpointed the problem. Here is a little test case: import static org.junit.Assert.assertEquals; import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl; import org.junit.Test; public class ConfigurationTest { @Test public void cloneTest() { JDBCConfigurationImpl config = new JDBCConfigurationImpl(); JDBCConfigurationImpl newConfig = (JDBCConfigurationImpl) config.clone(); assertEquals(config, newConfig); } } The warning message about the configuration property openjpa.jdbc.SynchronizeMappings is logged during config.clone(). clone() is implemented by invoking first toProperties(true) and then fromProperties() on the resulting properties map. fromProperties() removes all known properties from the map and then issues warnings for all remaining ones - at least that seems to be the intention. The problem is that SynchronizeMappings has no default value, or rather a default value of null. Thus, the map generated by toProperties(true) contains the key openjpa.jdbc.SynchronizeMappings with a value of null. fromProperties() in fact only removes the known properties with a non-null value, so the key openjpa.jdbc.SynchronizeMappings remains in the map and causes the warning. The warning would not appear if clone() invoked toProperties(false), but I'm not sure if this has any side effects. Moreover, in my test case, the assertion assertEquals(config, newConfig); fails - now the Javadoc of Object.clone() states that x.clone().equals(x) is not an absolute requirement (without giving any good reasons), but in this case I would definitely expect the clone of a configuration to be equal to the original. The assertion failure is caused by LockTimeout:0[int] != LockTimeout:0[int], so it appears that for x of class IntValue the invariant x.clone().equals(x) does not hold either (why?). Incidentally, the comment of ConfigurationImpl.toProperties() is outdated. It references a non-existing parameter getAll and a non-existing method getAllProperties(). Hopefully, this is enough input for someone familiar with the implementation details to come up with a fix... Best regards, Harald -- View this message in context: http://openjpa.208410.n2.nabble.com/Confusing-warning-about-openjpa-jdbc-SynchronizeMappings-tp5495438p5507719.html Sent from the OpenJPA Users mailing list archive at Nabble.com.