添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
I'm a bit new to this and I was doing some unit testing and I found something which totally threw me. I can't explain or work out what the problem might be. Can anyone help me?
Near the bottom in the testOperation() unit test I iterate through the elements of a set and find the element by doing an equals(). The set only has 1 element so it's very basic. Then I do a contains() but that fails???



The output looks like this:

17211 openjpa TRACE [main] openjpa.jdbc.SQL - <t 1641449698, conn 1645997488> executing prepstmnt 1372765087 SELECT NEXTVAL('webscore.OPERATION_ID_SEQ')
17216 openjpa TRACE [main] openjpa.jdbc.SQL - <t 1641449698, conn 1645997488> [4 ms] spent
17310 openjpa TRACE [main] openjpa.jdbc.SQL - <t 1641449698, conn 1848398916> executing prepstmnt 395440162 SELECT NEXTVAL('webscore.OPERATION_ID_SEQ')
17312 openjpa TRACE [main] openjpa.jdbc.SQL - <t 1641449698, conn 1848398916> [2 ms] spent
17411 openjpa TRACE [main] openjpa.jdbc.SQL - <t 1641449698, conn 242597432> executing prepstmnt 1440018046 INSERT INTO webscore.Operation (id, surgery_id) VALUES (?, ?) [params=(long) 25, (long) 42]
17418 openjpa TRACE [main] openjpa.jdbc.SQL - <t 1641449698, conn 242597432> [6 ms] spent
17418 openjpa TRACE [main] openjpa.jdbc.SQL - <t 1641449698, conn 242597432> executing prepstmnt 1319611905 INSERT INTO webscore.Operation (id, surgery_id) VALUES (?, ?) [params=(long) 26, (long) 43]
17420 openjpa TRACE [main] openjpa.jdbc.SQL - <t 1641449698, conn 242597432> [1 ms] spent
1:com.hjb.socrates.jpa.Operation@13b4cd65[surgery=com.hjb.socrates.jpa.Surgery@22cb1edc[name=My first surgery,password=password,licenses=[],operations=[com.hjb.socrates.jpa.Operation@13b4cd65[surgery=com.hjb.socrates.jpa.Surgery@22cb1edc,protocols=[],id=25]],id=42],protocols=[],id=25]
2:equals, com.hjb.socrates.jpa.Operation@13b4cd65[surgery=com.hjb.socrates.jpa.Surgery@22cb1edc[name=My first surgery,password=password,licenses=[],operations=[com.hjb.socrates.jpa.Operation@13b4cd65[surgery=com.hjb.socrates.jpa.Surgery@22cb1edc,protocols=[],id=25]],id=42],protocols=[],id=25]
3:not contained


The output from the unit test looks like this:

java.lang.AssertionError:
at org.junit.Assert.fail(Assert.java:91)
at org.junit.Assert.assertTrue(Assert.java:43)
at org.junit.Assert.assertTrue(Assert.java:54)
at com.hjb.socrates.test.jpa.Entities.testOperation(Entities.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Recently we too have encountered similar problem, I think when an element is added to the Set, it performs some hash on the object to find the bucket.
Even though equals and hashcode is same for object that is present in the set and the one which we are passing to it, it is not able to locate. both contains(), remove() is failing.

I guess it is not able to locate the object in a set once any of its id attributes are changed.

Someone please shed some light on this.

Thanks.
Srikanth.N
Note that [ code ] is the code tag here and not < code >. I've fixed your post for you.

Also, I'm moving this to our ORM forum since it is about JPA.