Weird Hibernate WrongClassexception

xiaoxiao2021-03-06  40

There are four types A, B, B, B1, B2, B1, B2 inheritance B, A and B1, B2 are relationship: code is as follows (omitting getter / setter): //a.javapublic class a { Private integer ID; private b1 b1; private b2 b2;}

//B.javapublic Abstract Class B {Private Integer ID; Private A A;

//B1.javapublic class b1 extends b {}

//B2.javapublic class b2 extends b {}

Hibernate mapping file is:

Use the Hibernate SchemauPdate to generate a table and insert the data of the following structure: Table: A ID1 table: B id b_type a_id 1 1 1 2 2 1

Then the A object of select id = 1, hibernate throws an exception: net.sf.hibernate.wrongClassexception: Object with ID: 1 WAS NOT OF TESER SPECified Subclass: Test.b2 (Loaded Object Was of Wrong Class) seems to be Hiberate The two records of id = 1 and ID = 2 are treated as an object (either B1, either b2), in this case, Hibernate is fully capable (only the type in the WHERE statement ) Distinguish our object is B1 or B2, don't know if this is not a bug of hibernate?

Workaround: Method 1: Spread A to B1, B2 relationship into the collection relationship between B. Method 2: Use Joined-SubClass definitions B1 and B2 to avoid two types in the same table

转载请注明原文地址:https://www.9cbs.com/read-52705.html

New Post(0)