Friday, 15 August 2014

java - Random LazyInitializationException with @OneToMany -


I have a legacy database with the following tables:

  Police ID (PK)  

I have a JPA unit Police

  @Entity Public Category Police Serializable { @ ID Private Long ID @ on Tommy (Fetch = Frettype.LAGY) @Join column (value = {@Join column (name = "id", referenced column name = "id")}) Private set & lt; Contract & gt; Contracts; }  

The contract unit looks like this:

@Entiree Public class contract implementation serializable {@ id private long ID; @ ID Private Long Version; Private string type; @OneToMany (fetch = FetchType.LAZY) @JoinColumn ({@JoinColumn (name = "code", referenced column name = "type")}) @ where (segment = "tab = 'type'") private set & lt; Code & gt; type; }

Code:

  @Entii public class code applies the Serializable {@Id Private String tab; @ Id personal string code; The name of the private string; } In the code table there are many different keys / values ​​for different applications in my case where I need one, from my contract "tab = 'type'" and code = Type is required.  

My problem is that if I have more than one contract for the police, then I'm Randoli? In my testcase, I do the following:

  public static zero main (string [] args) {int countErrors = 0; {Try {POLICE (;); for int i = 0; i & lt; 15; i ++); } Hold (exception e) {e.printStackTrace (); CountErrors ++; }} System.err.println ("Errors:" + Counter Errors); } Private static zero read policy () throws an exception {EntityManagerFactory Factory = EntityManagerFactoryHelper.getFactory (Continuity Suggestion TEST_STAGE); EntityManager em = factory.createEntityManager (); TypedQuery & LT; Police & gt; NameQuery = em.createNamedQuery (...); The result of police = has been nominated. Gatinglingericelt (); Set up & lt; Contract & gt; Contract = results. Gate contract (); Contract Contract = Contract Director () Next (); Set up & lt; Code & gt; Type = contract .getType (); // There should be a set with an entry system. Out.println (type.size ()); // & lt; --- The possibility for the exception !! Em.close (); }  

I try to see this whole thing in loop 15 times. In about 5-8 attempts, I get the Lease Initialization exception. The second time it works.

Any thoughts about this? Why not fail it all the time?

Just encountered this one. Being the key word 'random' one of my colleagues faced this exception on my laptop constantly, while I never did that. He can only generate exceptions on IE & amp; Edge

Finally realized that it was supposed to do with tomat versions, he was running an old version, while I had a later version - 8.5.8. He upgraded local Tomcat to this version and the exception was not encountered.


No comments:

Post a Comment