JPA Secondary Table XML Config - jpa

I am working on a project where I need to store an Entity, which is unmodifiable (from library), so I am using XML entity mappings entity object is like:
public abstract class BaseModel{
Long uid;
//getters/setters
}
public abstract class LocaleBaseModel{
String locale;
//Other properties, getter/setters
}
public class Article extends LocaleBaseModel{
private Long authorId;
privaet String text;
//Other properties
}
<mapped-superclass class="package.BaseModel">
<attributes>
<id name="uid">
<column name="UID" nullable="false" />
<generated-value strategy="AUTO" />
</id>
</attributes>
</mapped-superclass>
<mapped-superclass class="package.LocaleBaseModel">
<secondary-table name="STableName">
<primary-key-join-column name="MID" referenced-column-name="UID" />
</secondary-table>
<attributes>
<basic name="locale">
<column name="LOCALE" updatable="false" nullable="false" />
</basic>
<basic name="text">
<column name="Text" updatable="false" nullable="false" table="STableName" />
</basic>
</attributes>
</mapped-superclass>
<entity class="package.child.Article" name="Hadith">
<table name="TableName" />
<attributes>
</attributes>
</entity>
but as soon as I add <secondarytable> I get an error which is quiet explanatory in itself:
14:58:47,379 ERROR ErrorLogger:57 - Error parsing XML (2) :
cvc-complex-type.3.1: Value '2.0' of attribute 'version' of element
'entity-mappings' is not valid with respect to the corresponding
attribute use. Attribute 'version' has a fixed value of '1.0'.
14:58:47,379 ERROR ErrorLogger:57 - Error parsing XML (2) :
cvc-complex-type.2.4.a: Invalid content was found starting with
element 'element-collection'. One of
'{"http://java.sun.com/xml/ns/persistence/orm":basic,
"http://java.sun.com/xml/ns/persistence/orm":version,
"http://java.sun.com/xml/ns/persistence/orm":many-to-one,
"http://java.sun.com/xml/ns/persistence/orm":one-to-many,
"http://java.sun.com/xml/ns/persistence/orm":one-to-one,
"http://java.sun.com/xml/ns/persistence/orm":many-to-many,
"http://java.sun.com/xml/ns/persistence/orm":embedded,
"http://java.sun.com/xml/ns/persistence/orm":transient}' is expected.
14:58:47,379 ERROR ErrorLogger:57 - Error parsing XML (2) :
cvc-complex-type.2.4.a: Invalid content was found starting with
element 'secondary-table'. One of
'{"http://java.sun.com/xml/ns/persistence/orm":description,
"http://java.sun.com/xml/ns/persistence/orm":id-class,
"http://java.sun.com/xml/ns/persistence/orm":exclude-default-listeners,
"http://java.sun.com/xml/ns/persistence/orm":exclude-superclass-listeners,
"http://java.sun.com/xml/ns/persistence/orm":entity-listeners,
"http://java.sun.com/xml/ns/persistence/orm":pre-persist,
"http://java.sun.com/xml/ns/persistence/orm":post-persist,
"http://java.sun.com/xml/ns/persistence/orm":pre-remove,
"http://java.sun.com/xml/ns/persistence/orm":post-remove,
"http://java.sun.com/xml/ns/persistence/orm":pre-update,
"http://java.sun.com/xml/ns/persistence/orm":post-update,
"http://java.sun.com/xml/ns/persistence/orm":post-load,
"http://java.sun.com/xml/ns/persistence/orm":attributes}' is expected.
I have following on path, with others from spring 3.0.5
hibernate-entitymanager-3.6.2
hibernate-jpa-2.0-api
hibernate-core-3.6.2.Final

according to the orm-schema
the secondary-table elements likes to be placed
withing the
entity element after the table tag
<pre> alike <entity>
<table/>
<secondary-table/>
...
agreed the error message is noisy but that is
the glorious schema spec.
ys.

Related

Hibernate exception javassist

I need some help as I'm stumped on an Hibernate 4/Struts 2 project. This is my first Hibernate 4 (4.3.11) project, as I worked for years with Hibernate 3. The database is MySQL 5.
All mapping classes were produced with Hibernate Tools provided by JBoss Tools 4.3.5, on Eclipse Mars 2. No problem was encountered, it worked fine.
But when testing, I faced this exception :
2017-02-16 17:23:45 ERROR Dispatcher:38 - Exception occurred during processing request: metier.Ville_$$_javassist_2 cannot be cast to javassist.util.proxy.Proxy
java.lang.ClassCastException: metier.Ville_$$_javassist_2 cannot be cast to javassist.util.proxy.Proxy
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:147)
at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:75)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:771)
I read that post but I don't understand what's happening.
Two classes are involved : Salle and Ville. Here's the Hibernate XML files : Salle.hbm.xml & Ville.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">
<!-- Generated 2 f?vr. 2017 11:28:16 by Hibernate Tools 4.3.5.Final -->
<hibernate-mapping>
<class name="metier.Salle" table="salle" catalog="aevbadherents" optimistic-lock="version">
<id name="idSalle" type="int">
<column name="idSALLE" />
<generator class="assigned" />
</id>
<many-to-one name="ville" class="metier.Ville" fetch="select">
<column name="idVilleSalle" />
</many-to-one>
<property name="adresse1" type="string">
<column name="Adresse1" length="80" />
</property>
<property name="adresse2" type="string">
<column name="Adresse2" length="80" />
</property>
<property name="description" type="string">
<column name="Description" length="500" />
</property>
<set name="courses" table="cours" inverse="true" lazy="true" fetch="select">
<key>
<column name="IdSalle" not-null="true" />
</key>
<one-to-many class="metier.Cours" />
</set>
<set name="passagegrades" table="passagegrade" inverse="true" lazy="true" fetch="select">
<key>
<column name="IdSalle" />
</key>
<one-to-many class="metier.Passagegrade" />
</set>
</class>
</hibernate-mapping>
The second file :
<hibernate-mapping>
<class name="metier.Ville" table="ville" catalog="aevbadherents" optimistic-lock="version">
<id name="idVille" type="int">
<column name="idVILLE" />
<generator class="assigned" />
</id>
<many-to-one name="departement" class="metier.Departement" fetch="select">
<column name="idDepartement" not-null="true" />
</many-to-one>
<property name="nom" type="string">
<column name="Nom" length="60" not-null="true" />
</property>
<property name="codepostal" type="string">
<column name="codepostal" length="6" not-null="true" />
</property>
<property name="gpslat" type="string">
<column name="GPSLat" length="12" not-null="true" />
</property>
<property name="gpslon" type="string">
<column name="GPSLon" length="12" not-null="true" />
</property>
<set name="adherentsForIdVilleResid" table="adherent" inverse="true" lazy="true" fetch="select">
<key>
<column name="IdVilleResid" />
</key>
<one-to-many class="metier.Adherent" />
</set>
<set name="clubs" table="club" inverse="true" lazy="true" fetch="select">
<key>
<column name="idville" not-null="true" />
</key>
<one-to-many class="metier.Club" />
</set>
<set name="salles" table="salle" inverse="true" lazy="true" fetch="select">
<key>
<column name="idVilleSalle" />
</key>
<one-to-many class="metier.Salle" />
</set>
<set name="adherentsForIdVilleNais" table="adherent" inverse="true" lazy="true" fetch="select">
<key>
<column name="IdVilleNais" />
</key>
<one-to-many class="metier.Adherent" />
</set>
</class>
</hibernate-mapping>
I read that this exception may be caused by a JAR conflict, but I did not find anything.I don't use Maven (maybe I should ?), so let me show you the JARs included :
antlr-2.7.7.jar
commons-fileupload-1.3.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.2-javadoc.jar
commons-logging-1.2.jar
dom4j-1.6.1.jar
freemarker-2.3.19.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.11.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
log4j-1.2.15.jar
mysql-connector-java-5.1.40-bin.jar
ognl-3.0.6.jar
struts2-core-2.3.15.3.jar
truc.txt
xwork-core-2.3.15.3.jar
Please help me, as I'm stumped...
One of the possible cause for this problem is that you have in your classpath several versions of the same class.
I have been looking in java2s.com and, none of the jar you listed, seems to contain the class javassist.util.proxy.Proxy, so I will try to find if any other jar contains that class using jarscan with the following command:
jarscan -d PATH_TO_YOUR_CLASSPATH_DIR -j javassist.util.proxy.Proxy
You can download jarscan from here https://java.net/projects/jarscan/downloads
If you found that several packages contains the same class, you are done.
Malaguna,
Thanks for your help. Here's the result of jarscan :
D:\temp\java\jarscan>jarscan -j javassist.util.proxy.Proxy
......................
+javassist-3.18.1-GA.jar
javassist-3.18.1-GA.jar\javassist/util/proxy/Proxy.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$1.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$2.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$3.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$ClassLoaderProvide
r.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$Find2MethodsArgs.c
lass
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$ProxyDetails.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$UniqueName.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyObject.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyObjectInputStream.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyObjectOutputStream.class
----------------------------------------------
Scanned archives: 22
Errors: 0
Archives with hits: 12
So, it means that no other jar contains that class.

openjpa enhancer fails on one-to-many, but only if its after one-to-one?

I get the following exception when I have my open JPA entity mapping set up with a one-to-many mapping listed after a one-to-one mapping. When I switch the order, I don't get the exception.
I'm assuming that there is an XSD rule being applied, but what is the purpose of that rule?
Failed to execute goal
org.apache.openjpa:openjpa-maven-plugin:2.2.2:enhance (enhancer) on
project: Execution enhancer of goal
org.apache.openjpa:openjpa-maven-plugin:2.2.2:enhance failed:
org.xml.sax.SAXException: Invalid content was found starting with
element 'one-to-many'. One of
'{"http://java.sun.com/xml/ns/persistence/orm":one-to-one,
"http://java.sun.com/xml/ns/persistence/orm":many-to-many,
"http://java.sun.com/xml/ns/persistence/orm":element-collection,
"http://java.sun.com/xml/ns/persistence/orm":embedded,
"http://java.sun.com/xml/ns/persistence/orm":transient}' is expected.
<!-- FAILED -->
<entity class="com.test.comm">
<table schema="dbo" name="tbl_comm_data"/>
<attributes>
<id name="commId">
<column name="comm_id"/>
</id>
<basic name="commName">
<column name="comm_name"/>
</basic>
<one-to-one name="CommType" target-entity="com.test.TblCommType" mapped-by="TblComm" fetch="LAZY">
<cascade>
<cascade-merge/>
</cascade>
</one-to-one>
<one-to-many name="CommDtls" target-entity="com.test.TblCommDtl" mapped-by="tblCommFreq">
<cascade>
<cascade-merge/>
</cascade>
</one-to-many>
</attributes>
</entity>
<!-- WORKED -->
<entity class="com.test.comm">
<table schema="dbo" name="tbl_comm_data"/>
<attributes>
<id name="commId">
<column name="comm_id"/>
</id>
<basic name="commName">
<column name="comm_name"/>
</basic>
<one-to-many name="CommDtls" target-entity="com.test.TblCommDtl" mapped-by="tblCommFreq">
<cascade>
<cascade-merge/>
</cascade>
</one-to-many>
<one-to-one name="CommType" target-entity="com.test.TblCommType" mapped-by="TblComm" fetch="LAZY">
<cascade>
<cascade-merge/>
</cascade>
</one-to-one>
</attributes>
</entity>

Exception while parsing metadata in BreezeJS client

Prequel and version info:
BreezeJS client version: 1.5.2
Metadata generated on custom server via design-time EntityFramework model (6.1.2)
BreezeLabs EdmBuilder (1.0.5)
Had problems generating metadata also which can be read here:
How to generate valid metadata from EF DBContext via EdmBuilder for OData v3/BreezeJS
Question/Problem:
Now the breeze client is throwing an error while interpreting the metadata.
Unable to process returned metadata: A nonnullable DataProperty cannot have a null defaultValue. Name: MySuperDuperEnum
The exception occurs in breeze.debug.js at line 7110 - function parseCsdlSimpleProperty(parentType, csdlProperty, keyNamesOnServer).
Whats going wrong here? Do I have wrong metadata?
Here is the metadata:
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices m:DataServiceVersion="3.0" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<Schema Namespace="ODataMetaDataGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityType Name="MyObject" p5:ClrType="GreatPersistencyNamespace.Model.GreatStuff.MyObject, BreezeEval, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:p5="http://schemas.microsoft.com/ado/2013/11/edm/customannotation">
<Key>
<PropertyRef Name="ID"/>
</Key>
<Property Name="ID" Nullable="false" Type="Edm.Int32" p7:StoreGeneratedPattern="Identity" xmlns:p7="http://schemas.microsoft.com/ado/2009/02/edm/annotation"/>
<Property FixedLength="false" MaxLength="Max" Name="Name" Type="Edm.String" Unicode="true"/>
<Property Name="IsActive" Nullable="false" Type="Edm.Boolean"/>
<Property Name="IsGreat" Nullable="false" Type="Edm.Boolean"/>
<Property FixedLength="false" MaxLength="Max" Name="Description" Type="Edm.String" Unicode="true"/>
<Property Name="Address" Nullable="false" Type="ODataMetaDataGenerator.Address"/>
<Property Name="BehaviourType" Nullable="false" Type="ODataMetaDataGenerator.BehaviourType"/>
</EntityType>
<EnumType Name="BehaviourType" UnderlyingType="Edm.Byte" p5:ClrType="GreatPersistencyNamespace.Model.GreatStuff.BehaviourType, BreezeEval, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:p5="http://schemas.microsoft.com/ado/2013/11/edm/customannotation">
<Member Name="RUDE" Value="1"/>
<Member Name="KIND" Value="2"/>
<Member Name="UNKNOWN" Value="3"/>
</EnumType>
<EntityContainer Name="TheGreatModelContainer" p5:UseClrTypes="true" xmlns:p5="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" >
<EntitySet EntityType="ODataMetaDataGenerator.MyObject" Name="MyObjects"/>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
The exact source of the error is in breeze.debug.js at line 8597 - constructor of DataProperty.
There the defaultValue has to be set and in my case it tries to get the default value from the property dataType.
The dataType at this point looks the following:
{
name: "Undefined",
defaultValue: undefined
}
This happens due to a failure in line 7091 - var dataType = DataType.fromEdmDataType(csdlProperty.type);
because csdlProperty looks like:
{
name: "BehaviourType",
nullable: undefined
type: "ODataMetaDataGenerator.BehaviourType"
}
Which of course is not an Edm-Type and thus fails.. Should the underlying type of the enum be used here?

OpenJPA orphan removal not working

I am trying out a master child test case with a OneToMany unidirectional relationship.
I have a scenario where I want to update the master by removing some entries from the children list. When I do a merge, I want these child entries to be automatically deleted.
I have tried in vain to get this working. I tried orphanremoval too, but that too didn't help. There were some suggestions to add equals and hashcode, which didn't work either.
Any hint or help in this regard is highly appreciated. Thank you.
I am using JPA2.0 with OpenJPA 2.1.0
Here is my code.
//Parent class
public class Account {
private String accountId;
private String accountNumber;
private List<SubAccount> subAccounts;
//followed by getters/setters
}
//Child class
public class SubAccount {
private String subAccountId;
private String subAccountNumber;
//followed by getters/setters
}
My orm.xml is as follows.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<entity-mappings version="2.0"
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_2_0.xsd">
<entity name="Account" class="test.data.Account">
<table name="ACCOUNT" />
<sequence-generator name="AccountSeq"
sequence-name="ACCOUNT_TEST_SEQ" />
<attributes>
<id name="accountId">
<column name="ACCOUNT_ID" />
<generated-value strategy="SEQUENCE" generator="AccountSeq" />
</id>
<basic name="accountNumber">
<column name="ACCOUNT_NO" />
</basic>
<one-to-many name="subAccounts" fetch="EAGER"
target-entity="test.data.SubAccount" orphan-removal="true" >
<join-column name="ACCOUNT_ID" referenced-column-name="ACCOUNT_ID" nullable="false" updatable="true"/>
<cascade>
<cascade-all />
</cascade>
</one-to-many>
</attributes>
</entity>
<entity name="SubAccount" class="test.data.SubAccount">
<table name="SUBACCOUNT" />
<sequence-generator name="SubAccountSeq"
sequence-name="SUBACCOUNT_TEST_SEQ" />
<attributes>
<id name="subAccountPK">
<column name="SUBACCOUNT_ID" />
<generated-value strategy="SEQUENCE" generator="AccountSeq" />
</id>
<basic name="subAccountNumber">
<column name="SUBACCOUNT_NO" />
</basic>
</attributes>
</entity>
</entity-mappings>
And this is what I am trying to do.
Account acc = (Account)entityMgr.find(Account.class, "1100")
acc.getSubAccounts().remove(0);
entityMgr.merge(acc)
I expect the subaccount to be deleted.
OpenJPA overrides the orphan-removal behavior if the cascade type is set to cascade-all or cascade-remove.
Instead of cascade-all, I had to configure cascade-type with cascade-persist, cascade-merge, cascade-refresh and cascade-detach.

ef error:{"A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ShippingCardID'."}

I work in EF.Xml definition table is ...
<EntityType Name="ShippingCards">
<Key>
<PropertyRef Name="ShippingCardID" />
</Key>
<Property Name="ShippingCardID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="ProducedID" Type="int" />
<Property Name="User" Type="int" />
<Property Name="Count" Type="int" />
</EntityType>
methode is definition
public void addProduct(Product product)
{
ShippingCard sc = new ShippingCard();
sc.ProducedID = product.ProductID;
Add(sc);
context.SaveChanges();
}
When you try to put in the product in the cart breaks my mistake
{"A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ShippingCardID'."}
InnerError:
{"A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ShippingCardID'."}
You have incorrectly defined relations between entities. This error says that ShippingCardID is used as foreign key (= dependent property) in some relation which is not allowed because only properties with StoreGeneratedPattern.None can be used as foreign keys.