You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Werewolf <ne...@hotmail.com> on 2010/11/11 10:15:28 UTC
Parent reference is loaded, but disappears while working with the
object.
Hi @all,
i have a Model of 3 Classes:
InstanceItem, EntityClass and Parameter.
an InstanceItem has a reference to its parent InstanceItem and a Map to its
EntityClasses.
An EntityClass has a Reference to its InstanceItem and to a Collection of
Parameters.
Parameters have no references to anything, they just have some values.
My Problem:
The InstanceItem Reference to its parent gets lost somehow.
I have a breakpoint that stops when i load an InstanceItem without a parent
(i don't load the head elements).
I also have a breakpoint when the parent is accessed and it is null.
for checking that the programm does not change the InstanceItem, i added
Break points to the accessors for the parent InstanceItem and a breakpoint
for a field change to null.
I only hit the second breakpoint always... so somewhere the parent reference
disappears.
maybe there is too much data in the openJPA cache so that it throws away
something? i don't know what could happen there.
i add my mapping.xml file and my persistence.xml
if you need more info, please don't hesitate to ask.
would be nice to get that problem solved quickly.
MAPPING:
<?xml version="1.0" encoding="Cp1252" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">
<persistence-unit-metadata>
<xml-mapping-metadata-complete/>
<!--<persistence-unit-defaults>
<schema>yp</schema>
</persistence-unit-defaults>-->
</persistence-unit-metadata>
<mapped-superclass class="com.adva.yp.api.model.YPDatabaseItem"
metadata-complete="true"><!--
<entity-listeners>
<entity-listener
class="com.adva.yp.api.data_accessor.database_accessor.entity_listener.YPDatabaseItemEntityListener"/>
</entity-listeners>-->
<attributes>
<id name="id">
<generated-value strategy="IDENTITY"/>
</id>
<version name="dbVer"/>
<transient name="logger"/>
</attributes>
</mapped-superclass>
<!-- YPInstanceItem <-> YP_INSTANCE -->
<entity class="com.adva.yp.api.model.YPInstanceItem">
<table name="yp_instance"> <!-- schema="yp"> -->
<unique-constraint>
<column-name>YP_INSTANCE_UNIQUE_ID</column-name>
</unique-constraint>
</table>
<!-- Primary Key -->
<attribute-override name="id">
<column name="YP_INSTANCE_ID"/>
</attribute-override>
<attributes>
<!-- Basic mappings -->
<basic name="uniqueId"><column name="YP_INSTANCE_UNIQUE_ID" /></basic>
<basic name="name"><column name="YP_INSTANCE_NAME"/></basic>
<basic name="instanceKey"><column name="YP_INSTANCE_KEY"/></basic>
<basic name="categoryTypeId"><column
name="YP_CATEGORY_TYPE_ID"/></basic>
<basic name="instanceTypeId"><column
name="YP_INSTANCE_TYPE_ID"/></basic>
<basic name="description"><column name="YP_INSTANCE_DESCRIPTION"
column-definition="VARCHAR(1000)"/></basic>
<basic name="resolved"><column name="YP_INSTANCE_RESOLVED"/></basic>
<basic name="hidden"><column name="YP_INSTANCE_HIDDEN"/></basic>
<!-- Hierarchy Mapping to parent and child instance items -->
<many-to-one name="instanceParent"
target-entity="com.adva.yp.api.model.YPInstanceItem" fetch="EAGER">
<!--optional="true" fetch="LAZY">-->
<join-column name="YP_INSTANCE_ID_PARENT"/>
<cascade> <cascade-persist/> <cascade-merge/> <cascade-refresh/>
</cascade>
</many-to-one>
<!-- All Entities -->
<one-to-many name="entities"
target-entity="com.adva.yp.api.model.YPEntityClass"
mapped-by="referencedInstanceItem" fetch="LAZY">
<map-key name="name" />
<cascade> <cascade-all/> </cascade>
</one-to-many>
<!--
<one-to-many name="childInstanceItems"
target-entity="com.adva.yp.api.model.YPInstanceItem" fetch="LAZY"
mapped-by="instanceParent">
<cascade> <cascade-remove/> </cascade>
</one-to-many>
-->
<!--<transient name="logger" />-->
</attributes>
</entity>
<!-- YPEntityClass <-> YP_ENTITY -->
<entity class="com.adva.yp.api.model.YPEntityClass"
metadata-complete="true">
<table name="yp_entity_fast"> <!-- schema="yp"> -->
<unique-constraint>
<column-name>YP_ENTITY_TYPE_ID</column-name>
<column-name>YP_INSTANCE_ID</column-name>
</unique-constraint>
</table>
<!-- Primary Key -->
<attribute-override name="id">
<column name="YP_ENTITY_ID"/>
</attribute-override>
<attributes>
<!-- Basic mappings -->
<basic name="name"><column name="YP_ENTITY_NAME"/></basic>
<basic name="entityClassTypeId"><column
name="YP_ENTITY_TYPE_ID"/></basic>
<!--<basic name="referencedInstanceItemId"><column
name="YP_INSTANCE_ID"/></basic>-->
<!-- referenced YPInstanceItem -->
<many-to-one name="referencedInstanceItem"
target-entity="com.adva.yp.api.model.YPInstanceItem" optional="false">
<join-column name="YP_INSTANCE_ID" nullable="false"/>
<cascade> <cascade-persist/> <cascade-merge/> <cascade-refresh/>
</cascade>
</many-to-one>
<!-- All Parameters -->
<many-to-many name="params"
target-entity="com.adva.yp.api.model.YPParameter" fetch="LAZY">
<join-table name="yp_entity_parameter_fast">
<join-column name="YP_ENTITY_ID"/>
<inverse-join-column name="YP_PARAMETER_ID"/>
<unique-constraint>
<column-name>YP_ENTITY_ID</column-name>
<column-name>YP_PARAMETER_ID</column-name>
</unique-constraint><!-- -->
</join-table>
<cascade> <cascade-persist/> <cascade-merge/> <cascade-refresh/>
</cascade>
</many-to-many>
<!--<transient name="logger" />-->
</attributes>
</entity>
<!-- YPParameter <-> YP_PARAMETER -->
<entity class="com.adva.yp.api.model.YPParameter">
<table name="yp_parameter"> <!-- schema="yp"> -->
<unique-constraint>
<column-name>YP_PARAMETER_TYPE_ID</column-name>
<column-name>YP_PARAMETER_VALUE_STRING</column-name>
</unique-constraint>
</table>
<!-- Primary Key -->
<attribute-override name="id">
<column name="YP_PARAMETER_ID"/>
</attribute-override>
<attributes>
<basic name="parameterTypeId"><column
name="YP_PARAMETER_TYPE_ID"/></basic>
<basic name="parameterName"><column name="YP_PARAMETER_NAME"/></basic>
<basic name="doubleValue"><column
name="YP_PARAMETER_VALUE_DOUBLE"/></basic>
<basic name="longValue"><column
name="YP_PARAMETER_VALUE_LONG"/></basic>
<basic name="stringValue"><column
name="YP_PARAMETER_VALUE_STRING"/></basic>
<basic name="rangeMin"><column
name="YP_PARAMETER_VALUE_RMIN"/></basic>
<basic name="rangeMax"><column
name="YP_PARAMETER_VALUE_RMAX"/></basic>
<!--<transient name="logger" /> -->
</attributes>
</entity>
<entity
class="com.adva.yp.api.data_accessor.database_accessor.util.YPParameterRankingTupel">
<table name="yp_parameter_ranking"/>
<attributes>
<id name="id"><column name="PRIM_KEY"/><generated-value
strategy="IDENTITY"/></id>
<basic name="type"><column name="YP_PARAMETER_NAME"/></basic>
<!--nsertable="false" updatable="false" nullable="false"/></basic>-->
<basic name="rankingValue"><column name="YP_PARAMETER_AVG_HIT"
insertable="false" updatable="false" nullable="false"/></basic>
</attributes>
</entity>
<entity class="com.adva.yp.api.data_cache.YPCacheObject">
<table name="yp_cache"/>
<attribute-override name="id">
<column name="YP_CACHE_PK"/>
</attribute-override>
<attributes>
<basic name="headKey"><column name="YP_CACHE_HEAD"/></basic>
<basic name="positiveKey"><column name="YP_CACHE_POS"/></basic>
<basic name="negativeKey"><column name="YP_CACHE_NEG"/></basic>
<basic name="hitCount"><column
name="YP_CACHE_HITCOUNT"/></basic>
<basic name="instanceItemUniqueId"><column
name="YP_INSTANCE_UNIQUE_ID"/></basic>
<one-to-one name="instanceItem"
target-entity="com.adva.yp.api.model.YPInstanceItem" fetch="EAGER">
<join-column name="YP_INSTANCE_ID"/>
<cascade> <!--<cascade-persist/> <cascade-merge/>-->
<cascade-refresh/> </cascade>
</one-to-one>
</attributes>
</entity>
</entity-mappings>
PERSISTENCE (Connection String is set by Properties):
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="FastAccessYPAPI">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<mapping-file>com/adva/yp/api/data_accessor/database_accessor/orm/orm_mysql_fast_access.xml</mapping-file>
<class>com.adva.yp.api.model.YPDatabaseItem</class>
<class>com.adva.yp.api.model.YPInstanceItem</class>
<class>com.adva.yp.api.model.YPParameter</class>
<class>com.adva.yp.api.model.YPEntityClass</class>
<properties>
<!--Apache OpenJPA doesn't have a built-in connection pool.
Apache DB connection pooling services are used. -->
<property name="openjpa.ConnectionDriverName"
value="org.apache.commons.dbcp.BasicDataSource"/>
<!--The connection properties (URL, user etc) are set
internally./-->
<property name="openjpa.Log" value="DefaultLevel=WARN"/>
<property name="openjpa.MetaDataFactory" value="jpa"/>
<property name="openjpa.jdbc.SchemaFactory"
value="native(ForeignKeys=true)"/>
<property name="openjpa.IgnoreChanges" value="true"/>
<property name="openjpa.Multithreaded" value="true"/>
<property name="openjpa.NontransactionalRead" value="true"/>
<property name="openjpa.NontransactionalWrite" value="false"/>
<property name="openjpa.Optimistic" value="true"/>
<property name="openjpa.jdbc.DBDictionary"
value="TableType=innodb"/>
</properties>
</persistence-unit>
</persistence>
It runs on MySQL 5.0.
--
View this message in context: http://openjpa.208410.n2.nabble.com/Parent-reference-is-loaded-but-disappears-while-working-with-the-object-tp5728025p5728025.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Parent reference is loaded, but disappears while working with
the object.
Posted by Pinaki Poddar <pp...@apache.org>.
> well in general it works... there were some calculations before using the
parent instance,
> but somewhen it stops because of a NullPointerException.
> it looks like a data container ran into an overflow, threw away some
> database objects and didn't reload
> them as they were accessed. as i wrote before: when i load them,
> everything is well referenced.
It is difficult for me to comment without understanding the problem further.
> if you think, using <inheritance> and <discriminator...> could help in
> that way,
JPA will call your intent of storing each derived type from a common base
type to separate tables as TABLE_PER_CLASS strategy. As that is not the
default strategy for inheritance mapping, there should be a <inheritance>
specification within the <entity>. <discriminator> is not relevant because
you are not using SINGLE_TABLE strategy where each record type must be
discriminated from other type stored in the same table.
> can you give an example, where i need to put this <inheritance> and
> <discriminator...> stuff?
> can you please make an example for the superclass and one derived class?
I can, but it will take a while as I am in the middle of something else.
Please browse OpenJPA test source codes -- it has examples of such
inheritance mapping with @MappedSuperclass.
> its openJPA 1.2.2 by the way...
That can be a reason -- remember fixing a significant error in
MappedSuperclass post 1.2.2 -- if possible try to switch to a 2.0 version to
check if things improve.
-----
Pinaki
--
View this message in context: http://openjpa.208410.n2.nabble.com/Parent-reference-is-loaded-but-disappears-while-working-with-the-object-tp5728025p5744493.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Parent reference is loaded, but disappears while working with
the object.
Posted by Werewolf <ne...@hotmail.com>.
Hi Pinaki,
the meaning of the YPDatabaseItem is to hide out all database relevant
variables like the primary key id and the db version field. further it
simplifies some methods in my reader and writer class, because i can use
generified access to the classes.
Each Class except YPDatabaseItem has its own table. openJPA built the design
for that database structure from this mapping file. i just refined the field
length and set some indexes... with other JPA frameworks it works also fine.
i just have these referencing problems with openJPA.
I intend to have a table for YPInstanceItem, YPEntityClass, YPParameter, the
association table between entity and parameter and the 2 other "single"
opbjects.
well in general it works... there were some calculations before using the
parent instance, but somewhen it stops because of a NullPointerException.
it looks like a data container ran into an overflow, threw away some
database objects and didn't reload them as they were accessed. as i wrote
before: when i load them, everything is well referenced. i have breakpoints
in setInstanceParent (never hit), field instanceParent changes (never hit)
and while loading i have a breakpoint that should hit, when instanceParent
is null but its no root element (also never hit).
i just suddenly hits a general NullPointerException breakpoint when a method
is trying to access a missing reference. the object is each time a different
one, so its not restricted to a special instance or object. when i look into
the database i always see the references well defined. i also tried to
exchange the JPA implementator. with e*** ;-) there are no problems like
this. anyways i need to get it running with openJPA.
newly occured is: the entities collection also disappeared?! so i tried a
synthetic test setup where i load many YPInstanceItem objects into a set and
print out in a loop if it has a parent or not and if it is a root element.
the first items were complete, but after some items (i guess 100... need to
count) the references were missing again... for each following object...
if you think, using <inheritance> and <discriminator...> could help in that
way,
can you give an example, where i need to put this <inheritance> and
<discriminator...> stuff? can you please make an example for the superclass
and one derived class?
its openJPA 1.2.2 by the way...
thanks a lot for helping!
Gerald
--
View this message in context: http://openjpa.208410.n2.nabble.com/Parent-reference-is-loaded-but-disappears-while-working-with-the-object-tp5728025p5740577.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Parent reference is loaded, but disappears while working with
the object.
Posted by Pinaki Poddar <pp...@apache.org>.
Hi Gerald,
> "inheritance reflected in mapping descriptors"
Generally for an inheritance hierarchy of persistent classes, one would use
<inheritance> and <discriminator-column/value> tag (though all of them are
defaulted). The default SINGLE_TABLE inheritance mapping strategy will store
all the derived types in a single table and identify record of a particular
type by the value of discriminator column.
For example, YP_INSTANCE_TYPE_ID may be gainfully used as a discriminator
for YPInstanceItem records. However, for a mere suggestion, given the wide
variance on persistent properties of the derived classes default
SINGLE_TABLE inheritance strategy may not be the best choice.
In fact I am bit perplexed by the intent behind target database mapping with
the combination of
a) default SINGLE_TABLE inheritance strategy
*and*
b) attribute-override of primary column for each derived type
*and*
c) each derived type declaring its own table.
My guess is SINGLE_TABLE inheritance strategy is not what you intend.
> the problem occurs very quick (after 10 minutes or ca. 100 selects).
Coming back to your question, does this imply that the problem of losing
parent reference is intermittent?
Or is every parent reference is lost?
Another minor observation: the mapping descriptor uses <generated-value
strategy="IDENTITY"/> for the primary identity field. In such case, setId()
method is not necessary and can even lead to application coding error.
-----
Pinaki
--
View this message in context: http://openjpa.208410.n2.nabble.com/Parent-reference-is-loaded-but-disappears-while-working-with-the-object-tp5728025p5740209.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Parent reference is loaded, but disappears while working with
the object.
Posted by Werewolf <ne...@hotmail.com>.
Hi Pinaki,
Thanks a lot for your answer.
Yes, you are right. The YPDatabaseItem is the superclass of YPInstanceItem,
YPEntityClass and YPParameter
here the Java Class for YPInstance ITem:
public class YPInstanceItem extends YPDatabaseItem
{
public static final YPInstanceItem NONE = new YPInstanceItem();
private short categoryTypeId;
private short instanceTypeId;
private String instanceKey;
private String name;
private String uniqueId;
private String description;
private Map<String, YPEntityClass> entities = new LinkedHashMap<String,
YPEntityClass>();
private YPInstanceItem instanceParent;
private boolean resolved = false;
private boolean hidden = false;
public YPInstanceItem()
{
this("", "", "", YPInstanceType.VARIANT, YPCategoryType.NOT_DEFINED);
}
public YPInstanceItem(String uniqueId,
String name,
String instanceKey,
YPInstanceType instanceType,
YPCategoryType categoryType)
{
this.uniqueId = uniqueId;
this.name = name;
this.instanceKey = instanceKey;
this.instanceTypeId = instanceType.getType();
this.categoryTypeId = categoryType.getType();
}
.
.
.
public YPInstanceItem getInstanceParent()
{
return instanceParent;
}
public void setInstanceParent(YPInstanceItem instanceParent)
{
this.instanceParent = instanceParent;
}
.
.
.
}
As you can see, the YPInstanceItem extends the YPDatabaseItem.
public abstract class YPDatabaseItem
{
private final Logger logger = Logger.getLogger(this.getClass());
private int id;
private int dbVer;
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
public int getDbVer() {
return dbVer;
}
public void setDbVer(int dbVer) {
this.dbVer = dbVer;
}
public abstract String getUniqueConstraintQueryString();
@Override
public abstract boolean equals(Object otherItem);
public abstract <T extends YPDatabaseItem> boolean similarInstance(T
otherItem);
final protected Logger getLogger()
{
return logger;
}
}
The YPDatabaseItem only has 2 persistent fields: id and dbVer. The
Superclass is mapped like this:
<mapped-superclass class="com.adva.yp.api.model.core.YPDatabaseItem"
metadata-complete="true">
<attributes>
<id name="id">
<generated-value strategy="IDENTITY"/>
</id>
<version name="dbVer"/>
<transient name="logger"/>
</attributes>
</mapped-superclass>
the ID is mapped in every derived class like this (example for
YPInstanceITem):
<attribute-override name="id">
<column name="YP_INSTANCE_ID"/>
</attribute-override>
and here is the table for YPInstanceItem:
delimiter $$
CREATE TABLE `yp_instance` (
`YP_INSTANCE_ID` int(11) NOT NULL auto_increment,
`DBVER` bigint(20) default NULL,
`YP_INSTANCE_TYPE_ID` int(11) default NULL,
`YP_INSTANCE_UNIQUE_ID` varchar(255) default NULL,
`YP_INSTANCE_KEY` varchar(255) default NULL,
`YP_CATEGORY_TYPE_ID` int(11) default NULL,
`YP_INSTANCE_RESOLVED` tinyint(1) default '0',
`YP_INSTANCE_DESCRIPTION` varchar(1000) default NULL,
`YP_INSTANCE_HIDDEN` tinyint(1) default '0',
`YP_INSTANCE_NAME` varchar(255) default NULL,
`YP_INSTANCE_ID_PARENT` int(11) default NULL,
PRIMARY KEY (`YP_INSTANCE_ID`),
UNIQUE KEY `UNQ_yp_instance_0` (`YP_INSTANCE_UNIQUE_ID`),
KEY `FK_yp_instance_YP_INSTANCE_ID_PARENT` (`YP_INSTANCE_ID_PARENT`),
KEY `YP_INSTANCE_IDX0`
(`YP_INSTANCE_TYPE_ID`,`YP_CATEGORY_TYPE_ID`,`YP_INSTANCE_HIDDEN`),
CONSTRAINT `FK_yp_instance_YP_INSTANCE_ID_PARENT` FOREIGN KEY
(`YP_INSTANCE_ID_PARENT`) REFERENCES `yp_instance` (`YP_INSTANCE_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1$$
i am not sure if i could answer to all of your questions. i don't know
exactly what you mean by "inheritance reflected in mapping descriptors" and
"Java domain class definitions". but i am sure i could satisfy your needs
with the parent relations of YPInstanceItem ;-)
maybe i need to add that comment: the entity manager is open for the
lifetime of the program and the problem occurs very quick (after 10 minutes
or ca. 100 selects).
Cheers
Gerald
--
View this message in context: http://openjpa.208410.n2.nabble.com/Parent-reference-is-loaded-but-disappears-while-working-with-the-object-tp5728025p5739938.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Parent reference is loaded, but disappears while working with
the object.
Posted by Pinaki Poddar <pp...@apache.org>.
I am guessing, from the descriptors, that YPDatabaseItem is the mapped super
class of InstanceItem, EntityClass etc. Is that right?
If yes, how is that inheritance reflected in mapping descriptors?
If not, can you post the Java domain class definitions?
Also, the code snippet that sets the parent relation of InstanceItem?
-----
Pinaki
--
View this message in context: http://openjpa.208410.n2.nabble.com/Parent-reference-is-loaded-but-disappears-while-working-with-the-object-tp5728025p5736023.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.