Java 如何将抽象类的ORM编写为超类(无注释)
我有一个名为User.java的类,它有一个活动列表。 Activity.java是由Dance.java和Drawing.java扩展的抽象类 以下是orm文件: user.orm.xmlJava 如何将抽象类的ORM编写为超类(无注释),java,hibernate,jpa,orm,eclipselink,Java,Hibernate,Jpa,Orm,Eclipselink,我有一个名为User.java的类,它有一个活动列表。 Activity.java是由Dance.java和Drawing.java扩展的抽象类 以下是orm文件: user.orm.xml <?xml version="1.0" encoding="UTF-8"?> <entity-mappings version="2.5" xmlns="http://www.eclip
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.5"
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_5.xsd">
<entity name="User" class="com.app001.demo.User">
<table name="USER" />
<attributes>
<embedded-id attribute-type="com.app001.demo.UserKey" name="userKey">
<attribute-override name="key">
<column name="USER_ID"/>
</attribute-override>
</embedded-id>
<!-- what do i put here for list of activities-->
</attributes>
</entity>
</entity-mappings>
dance.orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.5"
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_5.xsd">
<entity name="Dance" class="com.app001.demo.Dance">
<table name="DANCE" />
<attributes>
<embedded-id attribute-type="com.app001.demo.DanceKey" name="danceKey">
<attribute-override name="key">
<column name="DANCE_ID"/>
</attribute-override>
</embedded-id>
<basic attribute-type="java.lang.String" name="danceName">
<column name="DANCE_NAME"/>
</basic>
</attributes>
</entity>
</entity-mappings>
drawing.orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.5"
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_5.xsd">
<entity name="Drawing" class="com.app001.demo.Drawing">
<table name="DRAWING" />
<attributes>
<embedded-id attribute-type="com.app001.demo.DrawingKey" name="drawingKey">
<attribute-override name="key">
<column name="DRAWING_ID"/>
</attribute-override>
</embedded-id>
<basic attribute-type="java.lang.String" name="drawingName">
<column name="DRAWING_NAME"/>
</basic>
</attributes>
</entity>
</entity-mappings>
activity.orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.5"
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_5.xsd">
<!-- what do i put here for activity-->
我尝试了以下方法:
,这将不起作用,因为我不允许在
中输入任何
,将
标记为每个类的表或加入,我会得到以下错误:
在同步过程中,通过未标记为cascade PERSIST的关系找到了一个新对象注意我不能对java代码做太多修改。如果需要,我可以在Activity.java中添加一个id。您所说的“它将有一个活动类型的鉴别器列”是什么意思?如果将(新舞蹈()、新游戏()、新绘图())的列表设置为用户活动,会发生什么情况?请忽略鉴别器列部分。这是我这边的错误理解。根据您的问题,如果我们为一个用户存储多个活动,那么理想情况下,每个活动都将放在自己的表中,并且将用户id作为外键。但由于在用户和舞蹈/游戏/绘图表之间有一个抽象类(Activity.java),我不确定如何为Activity抽象类编写ORM。请注意,我无法更改java代码。到目前为止,您尝试了什么?使用的最安全策略是(至少需要id属性才能转到基本实体)。你可能也想尝试一下;虽然不确定Hibernate在这种情况下处理一对多关联的效果如何,但我在Activity类中添加了一个id属性。在activity.orm.xml中将继承策略标记为每个类的表。创建了一个舞蹈对象并将其设置为User.activities,然后执行
em.persist(User)
操作时出现以下错误:在同步过程中,通过未标记为cascade persist的关系找到了一个新对象。有什么想法吗?@crizzis我对整个问题进行了编辑,以使我的问题更清楚。请看一看。
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.5"
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_5.xsd">
<entity name="User" class="com.app001.demo.User">
<table name="USER" />
<attributes>
<embedded-id attribute-type="com.app001.demo.UserKey" name="userKey">
<attribute-override name="key">
<column name="USER_ID"/>
</attribute-override>
</embedded-id>
<!-- what do i put here for list of activities-->
</attributes>
</entity>
</entity-mappings>
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.5"
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_5.xsd">
<entity name="Dance" class="com.app001.demo.Dance">
<table name="DANCE" />
<attributes>
<embedded-id attribute-type="com.app001.demo.DanceKey" name="danceKey">
<attribute-override name="key">
<column name="DANCE_ID"/>
</attribute-override>
</embedded-id>
<basic attribute-type="java.lang.String" name="danceName">
<column name="DANCE_NAME"/>
</basic>
</attributes>
</entity>
</entity-mappings>
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.5"
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_5.xsd">
<entity name="Drawing" class="com.app001.demo.Drawing">
<table name="DRAWING" />
<attributes>
<embedded-id attribute-type="com.app001.demo.DrawingKey" name="drawingKey">
<attribute-override name="key">
<column name="DRAWING_ID"/>
</attribute-override>
</embedded-id>
<basic attribute-type="java.lang.String" name="drawingName">
<column name="DRAWING_NAME"/>
</basic>
</attributes>
</entity>
</entity-mappings>
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.5"
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_5.xsd">
<!-- what do i put here for activity-->