-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Problem with Session.load
PostPosted: Wed Jan 04, 2006 2:11 pm 
Newbie

Joined: Tue Dec 27, 2005 2:55 pm
Posts: 18
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Hello!

I have a strage problem here: I'm following the hibernate manual reference on chapter 1 where it says to Work with associations. So the code applied to my aplication context looks very like the same! Well the prob i'm facing is when I load the class Product: Product theProduct = (Product) session.load(Product.class, prodId); I get a NullPointerException. I have no idea how this came up, so I'd really apprecite some here on this.

Here the class Manager code:
NOTE: the db schema on this part is: TableProduct (1)----(*)IntermTableProductmparameter(*)----(1)TableMonitParameter

NOTE2: I generated the java classes with the last version of HibernateTools. (DB schema ->xml.hbm->hbm2java)


public class ProductManager {


public static void main(String[] args) {

ProductManager mgr = new ProductManager();


if (args[0].equals("inserir_produto")) {
mgr.createAndStoreProduct("PRD-03", "My Product3", "Steel");
}
else if (args[0].equals("listar")) {
List products = mgr.listProducts();
for (int i = 0; i < products.size(); i++) {
Product myProduct = (Product) products.get(i);
System.out.println("Product: " + myProduct.getProductId() +
" Name: " + myProduct.getName() + " Classification: " +
myProduct.getClassification());
}
}

else if (args[0].equals("addprodtoprodmparam")) {
Integer MonitParam = mgr.createAndStoreMonitorizationParameter
(4, "Motor Temperature", "Temperature of the motor", "Celcius", 90.0, 130.0, 50.0,
120.0, 60.0, 110.0, 70.0, 5.0, 1.0, 1.0, 10.0, 10.0);
String productId = mgr.createAndStoreProduct("PRD-04", "My Product4", "Plastic");
mgr.addProductToProductmparameter(productId, MonitParam);
System.out.println("Added product " + productId + " to productmparameter " + MonitParam);
}

HibernateUtil.getSessionFactory().close();
}


private String createAndStoreProduct(String prodId, String name, String classification) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try{
session.beginTransaction();

Product myProduct = new Product();
myProduct.setProductId(prodId);
myProduct.setName(name);
myProduct.setClassification(classification);

session.save(myProduct);
session.getTransaction().commit();
return myProduct.getProductId();
}
catch (RuntimeException e){
session.getTransaction().rollback();
System.err.println("Could not create and store product: " + e);
return null;
}
}



private Integer createAndStoreMonitorizationParameter
(Integer ParameterId, String name, String description, String measurementUnit, double avrgValue,
double maxValue, double minValue, double maxLimitAlarm,
double minLimitAlarm, double maxLimitWarning, double minLimitWarning,
double sampleInterval, double stsampleInterval, double stpredictionInterval,
double ltsampleInterval, double ltpredictionInterval) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try{
session.beginTransaction();

Monitorizationparameter myMonitorizationparam = new Monitorizationparameter();
myMonitorizationparam.setParameterId(ParameterId);
myMonitorizationparam.setName(name);
myMonitorizationparam.setDescription(description);
myMonitorizationparam.setMeasurementUnit(measurementUnit);
myMonitorizationparam.setAvrgValue(avrgValue);
myMonitorizationparam.setMaxValue(maxValue);
myMonitorizationparam.setMinValue(minValue);
myMonitorizationparam.setMaxLimitAlarm(maxLimitAlarm);
myMonitorizationparam.setMinLimitAlarm(minLimitAlarm);
myMonitorizationparam.setMaxLimitWarning(maxLimitWarning);
myMonitorizationparam.setMinLimitWarning(minLimitWarning);
myMonitorizationparam.setSampleInterval(sampleInterval);
myMonitorizationparam.setStsampleInterval(stsampleInterval);
myMonitorizationparam.setStpredictionInterval(stpredictionInterval);
myMonitorizationparam.setLtsampleInterval(ltsampleInterval);
myMonitorizationparam.setLtpredictionInterval(ltpredictionInterval);
//myMonitorizationparam.setMaintenanceobject(maintenanceobject);
//myMonitorizationparam.setSensor(sensor);

session.save(myMonitorizationparam);
session.getTransaction().commit();
return myMonitorizationparam.getParameterId();
}
catch (RuntimeException e){
session.getTransaction().rollback();
System.err.println("Could not create and store product: " + e);
return null;
}
}



private void addProductToProductmparameter(String prodId, Integer paramId) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

Product theProduct = (Product) session.load(Product.class, prodId);
Productmparameter prdmparam =
(Productmparameter) session.load(Productmparameter.class, paramId);

theProduct.getProductmparameters().add(prdmparam);
session.getTransaction().commit();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 6:12 am 
Newbie

Joined: Tue Dec 27, 2005 2:55 pm
Posts: 18
I'm still trying to understand this problem... the row of the productID i'm loading is created previously so it must load the info to instantiate the object!? Does anyone has a clue?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 12:56 pm 
Newbie

Joined: Tue Dec 27, 2005 2:55 pm
Posts: 18
I wonder if the problem is not in Java code but with the .hbm files generated by Hibernate Tools... because I noticed that there is no reference on "cascade" in any of them! So, probably the propagation can't be done at all!?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 1:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes, the tools does not configure cascading...no info in jdbc metadata to make a proper decision.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 06, 2006 12:31 pm 
Newbie

Joined: Tue Dec 27, 2005 2:55 pm
Posts: 18
Right so, I'm still having the same problem :S

I modified the hbm.xml of 2 table having a one-to-many association (Sensor 1 --- * MonitorizationParameter) and the look like this:


<hibernate-mapping>
<class
name="maintenance.Sensor"
table="sensor"
catalog="kobas_maintenance"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="sensorId"
column="SensorID"
type="integer"
>
<generator class="identity" />
</id>
<property
name="name"
type="string"
update="true"
insert="true"
access="property"
column="Name"
length="20"
unique="true"
/>

<property
name="family"
type="string"
update="true"
insert="true"
access="property"
column="Family"
length="20"
/>

<property
name="sensorType"
type="string"
update="true"
insert="true"
access="property"
column="SensorType"
length="20"
/>

<property
name="localization"
type="string"
update="true"
insert="true"
access="property"
column="Localization"
length="20"
/>

<property
name="brand"
type="string"
update="true"
insert="true"
access="property"
column="Brand"
length="20"
/>

<property
name="state"
type="string"
update="true"
insert="true"
access="property"
column="State"
length="2"
/>

<set
name="monitorizationparameters"
inverse="true"
table="kobas_maintenance.Monitorizationparameter"
lazy="true"
cascade="save-update"
>
<key
column="ID_Sensor"
>
</key>
<one-to-many
class="maintenance.Monitorizationparameter"
/>
</set>
<set
name="ocorrencyvalues"
inverse="true"
table="kobas_maintenance.Ocorrencyvalue"
lazy="true"
cascade="save-update"
>
<key
column="ID_Sensor" not-null="true"
>
</key>
<one-to-many
class="maintenance.Ocorrencyvalue"
/>
</set>
<set
name="ltpredictionvalues"
inverse="true"
table="kobas_maintenance.Ltpredictionvalue"
lazy="true"
cascade="save-update"
>
<key
column="ID_Sensor" not-null="true"
>
</key>
<one-to-many
class="maintenance.Ltpredictionvalue"
/>
</set>
</class>
</hibernate-mapping>

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

<hibernate-mapping>
<class
name="maintenance.Monitorizationparameter"
table="monitorizationparameter"
catalog="kobas_maintenance"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="parameterId"
column="ParameterID"
type="integer"
>
<generator class="identity" />
</id>
<many-to-one name="maintenanceobject" class="maintenance.Maintenanceobject" fetch="select">
<column name="ID_Object" length="20" />
</many-to-one>

<many-to-one name="sensor" class="maintenance.Sensor" fetch="select">
<column name="ID_Sensor" />
</many-to-one>

<property
name="name"
type="string"
update="true"
insert="true"
access="property"
column="Name"
length="20"
unique="true"
>
</property>

<property
name="description"
type="string"
update="true"
insert="true"
access="property"
column="Description"
length="100"
>
</property>

<property
name="measurementUnit"
type="string"
update="true"
insert="true"
access="property"
column="MeasurementUnit"
length="10"
>
</property>

<property
name="avrgValue"
type="double"
update="true"
insert="true"
access="property"
column="AvrgValue"
precision="22"
scale="0"
>
</property>

<property
name="maxValue"
type="double"
update="true"
insert="true"
access="property"
column="MaxValue"
precision="22"
scale="0"
>
</property>

<property
name="minValue"
type="double"
update="true"
insert="true"
access="property"
column="MinValue"
precision="22"
scale="0"
>
</property>

<property
name="maxLimitAlarm"
type="double"
update="true"
insert="true"
access="property"
column="MaxLimitAlarm"
precision="22"
scale="0"
>
</property>

<property
name="minLimitAlarm"
type="double"
update="true"
insert="true"
access="property"
column="MinLimitAlarm"
precision="22"
scale="0"
>
</property>

<property
name="maxLimitWarning"
type="double"
update="true"
insert="true"
access="property"
column="MaxLimitWarning"
precision="22"
scale="0"
>
</property>

<property
name="minLimitWarning"
type="double"
update="true"
insert="true"
access="property"
column="MinLimitWarning"
precision="22"
scale="0"
>
</property>

<property
name="sampleInterval"
type="double"
update="true"
insert="true"
access="property"
column="SampleInterval"
precision="22"
scale="0"
>
</property>

<property
name="stsampleInterval"
type="double"
update="true"
insert="true"
access="property"
column="STSampleInterval"
precision="22"
scale="0"
>
</property>

<property
name="stpredictionInterval"
type="double"
update="true"
insert="true"
access="property"
column="STPredictionInterval"
precision="22"
scale="0"
>
</property>

<property
name="ltsampleInterval"
type="double"
update="true"
insert="true"
access="property"
column="LTSampleInterval"
precision="22"
scale="0"
>
</property>

<property
name="ltpredictionInterval"
type="double"
update="true"
insert="true"
access="property"
column="LTPredictionInterval"
precision="22"
scale="0"
>
</property>

<set
name="reasonmparameters"
inverse="true"
table="kobas_maintenance.Reasonmparameter"
lazy="true"
cascade="save-update"
>
<key
column="ParameterID"
not-null="true"
>
</key>
<one-to-many
class="maintenance.Reasonmparameter"
/>
</set>
<set
name="productmparameters"
inverse="true"
table="kobas_maintenance.Productmparameter"
lazy="true"
cascade="save-update"
>
<key
column="ParameterID"
not-null="true"
>
</key>
<one-to-many
class="maintenance.Productmparameter"
/>
</set>
<set
name="warnings"
inverse="true"
table="kobas_maintenance.Warning"
lazy="true"
cascade="save-update"
>
<key
column="ID_Parameter"
>
</key>
<one-to-many
class="maintenance.Warning"
/>
</set>
<set
name="ambiencemparameters"
inverse="true"
table="kobas_maintenance.Ambiencemparameter"
lazy="true"
cascade="save-update"
>
<key
column="ParameterID"
not-null="true"
>
</key>
<one-to-many
class="maintenance.Ambiencemparameter"
/>
</set>
<set
name="processmparameters"
inverse="true"
table="kobas_maintenance.Processmparameter"
lazy="true"
cascade="save-update"
>
<key
column="ParameterID"
not-null="true"
>
</key>
<one-to-many
class="maintenance.Processmparameter"
/>
</set>
</class>
</hibernate-mapping>


Since I added cascade="save-update" to the respective set that links Sensor to MonitorizationParameter (name="monitorizationparameters") it should work, no!?

I also check the stack trace and I noticed an odd thing (that may be the origin of my problem). This message repeats for EVERY class:

ERROR BasicLazyInitializer:130 - CGLIB Enhancement failed: maintenance.Mwo
java.lang.NoClassDefFoundError
at org.hibernate.proxy.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:116)
at org.hibernate.proxy.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:41)
at org.hibernate.tuple.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:161)
at org.hibernate.tuple.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:131)
at org.hibernate.tuple.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55)
at org.hibernate.tuple.TuplizerLookup.create(TuplizerLookup.java:64)
at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:257)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:412)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:108)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:215)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1154)
at util.HibernateUtil.<clinit>(HibernateUtil.java:22)
at maintenance.MonitorizationParameterManager.createAndStoreSensor(MonitorizationParameterManager.java:127)
at maintenance.MonitorizationParameterManager.main(MonitorizationParameterManager.java:44)
16:17:06,984 WARN PojoEntityTuplizer:173 - could not create proxy factory for:maintenance.Mwo
org.hibernate.HibernateException: CGLIB Enhancement failed: maintenance.Mwo
at org.hibernate.proxy.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:132)
at org.hibernate.proxy.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:41)
at org.hibernate.tuple.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:161)
at org.hibernate.tuple.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:131)
at org.hibernate.tuple.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55)
at org.hibernate.tuple.TuplizerLookup.create(TuplizerLookup.java:64)
at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:257)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:412)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:108)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:215)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1154)
at util.HibernateUtil.<clinit>(HibernateUtil.java:22)
at maintenance.MonitorizationParameterManager.createAndStoreSensor(MonitorizationParameterManager.java:127)
at maintenance.MonitorizationParameterManager.main(MonitorizationParameterManager.java:44)
Caused by: java.lang.NoClassDefFoundError
at org.hibernate.proxy.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:116)
... 14 more

What's wrong with CGLIBLazyInitializer??? I added the cglib-2.1.3 jar to the lib folder together with the rest of the jars...

Help on this would really be much appreciated!! Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 11:10 am 
Newbie

Joined: Tue Dec 27, 2005 2:55 pm
Posts: 18
I'm sorry if I bother, but I really need help on this problem. I can't insent data in FK columns and in intermediate many-to-many tables! :'(


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 11:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
If your posting (or a question you are referring to) was not answered by anybody, the possible reasons are:

- http://www.hibernate.org/ForumMailingli ... AskForHelp
- You did not submit enough information
- Nobody knows the answer or has the free time to answer

What you can do now:

- Do the things listed in After Posting
- Add missing and/or more information
- Consider commercial support for guaranteed expert response times

This is a high-traffic forum run by volunteers with hundreds of postings made every day. The community works because people try to help others in their free time. Nobody is paid for this service or has to pay.

You should not expect a timely response and you should not rely on a public community forum for critical cases.

All community members should respect the rules of this forum and treat others like they would prefer to be treated.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 6:57 am 
Newbie

Joined: Tue Dec 27, 2005 2:55 pm
Posts: 18
Regarding max last post....I'm sorry, you're right. I'm just a regular newbie...

Now, for some developments:
I still have the same ERROR BasicLazyInitializer:130 - CGLIB Enhancement failed: maintenance.Warning java.lang.NoClassDefFoundError.
Although, I got a way to circle the problem by writing an HQL query to update the FK column of the child table. I'm not pleased with this solution because I want to be able to make Transitive Persistence, like the manual reference example in the 1st chapter.

Also, if someone could explain me very quickly what's the difference between Lazy Fetching and Eager (Outer Join) Fetching, and in what situations it's better to apply each of them.


Thanks in advance! [[]]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 3:11 pm 
Newbie

Joined: Tue Dec 27, 2005 2:55 pm
Posts: 18
Hi! I forgot to say: problem solved last week! Missing .jar :P

Anyway I have a doubt. Is it possible to create a MySQL (or whatever) database schema in the hibernate.cfg.xml file??

I'm thinking of instaling a new database schema after I install MySQL and then run the application where I define the connection, mappings and run hbm2ddl. The one thing left it's precisely the previous creation of the schema.

Any suggestions? :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 05, 2009 8:44 am 
Newbie

Joined: Thu Mar 05, 2009 2:50 am
Posts: 14
What is the Jar that you missed for solving the problem which Jar file did you add which solved your problem

_________________
Thanks and Regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.