java - hibernate simple inheritance - OR - xml property import/include -
final goal:
have few java objects sharing same base class persisted database while each 1 having own self-contained table own/inherited objects, , simple auto-generated database id.
very simple requirement. impossible (?) hibernate!
what have far (using mysql, hibernate xml mapping):
map.hbm.xml
<?xml version="1.0"?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping default-access="field" default-lazy="false"> <class name="xyz.url.hibernate.entitybase" abstract="true"> <id name="m_id" column="id"> <generator class="identity" /> </id> <version name="m_version" column="version" type="long" /> <union-subclass name="xyz.url.hibernate.user" table="my_entity"> <property name="name" column="name" type="string" /> </union-subclass> </class> </hibernate-mapping>
entitybase.java
public abstract class entitybase { private final long m_id; private final long m_version; public entitybase() { this.m_id = 0; this.m_version = 0; } public long get_id() { return this.m_id; } public long get_version() { return this.m_version; } }
user.java
public class user extends entitybase { public string name; }
the above not work unless change generator
class increment
.
given error:
org.hibernate.mappingexception: cannot use identity column key generation <union-subclass> mapping for: xyz.url.hibernate.user
well, why hibernate assumes want unique id in program-wide scope (i've read jpa requirement)... crap!
anyway, insist of having simple table (per object) aggregates object's (user
in case) properties, , deny using discriminators (again crap..) complicate final sql queries , hit performance.
the solutions see here:
manually map properties in 1 block inside xml.
map properties while "importing"
<propert>
items external file, achieve inheritance (reusage of properties). possible? how do?!?explore annotations further far i've seen don't support simple inheritance requirement.
dump hibernate , go orm solution.
please don't link docs - gave on 1 after reading them few times!
example of property import (from external file) great.
thanks , god bless!
first of need decide whether inheritance relationship should mapped database (to allow polymorphic queries such from entitybase
, polymorphic realtionships, etc) or not.
as far understand in case shouldn't mapped, therefore doesn't make sense use inheritance mapping options such <union-subclass>
@ all. have following options:
2. hibernate doesn't have special support reuse of xml mappings, documentation suggests use xml entities in case, see, example, 10.1.6. table per concrete class using implicit polymorphism.
3. annotations support requirement in form of @mappedsuperclass
annotation. annotation can used mark class not mapped database itself, mapping annotations defined on properties take effect mapped subclasses, don't need repeat them.
Comments
Post a Comment