-->
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.  [ 7 posts ] 
Author Message
 Post subject: Error when using private constructor
PostPosted: Mon Aug 22, 2005 2:42 pm 
Newbie

Joined: Mon Aug 22, 2005 2:34 pm
Posts: 4
I readed in the documentation that I can use a private constructor for a class. But I get an error if I do it.
The error is:
20:30:54,468 ERROR BasicLazyInitializer:103 - CGLIB Enhancement failed: bulldog.compta.domainobj.Epigrafe
java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

I attach the details:

Hibernate version: 3.0.5

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="bulldog.compta.domainobj.Epigrafe" table="EPIGRAFES">
<id name="id" column="ID" type="string">
<generator class="assigned" />
</id>
<many-to-one name="epigrafePadre" column="EPIGRAFEPADRE" class="bulldog.compta.domainobj.Epigrafe" />
<set name="epigrafesHijos" inverse="true" lazy="false" optimistic-lock="false">
<key column="EPIGRAFEPADRE"/>
<one-to-many class="bulldog.compta.domainobj.Epigrafe"/>
</set>
<set name="cuentasHijas" inverse="true" lazy="false" optimistic-lock="false">
<key column="EPIGRAFE"/>
<one-to-many class="bulldog.compta.domainobj.Cuenta"/>
</set>
<many-to-one name="ejercicio" column="EJERCICIO" class="bulldog.compta.domainobj.Ejercicio"/>
<property name="descripcion" column="DESCRIPCION" type="string"/>
<property name="formula" column="FORMULA" type="string"/>
</class>
</hibernate-mapping>

POJO class
public class Epigrafe implements Serializable
{
private String id;
private Ejercicio ejercicio;
private String descripcion;
private String formula;
private Epigrafe epigrafePadre;
private Set<Epigrafe> epigrafesHijos;
private Set<Cuenta> cuentasHijas;


private Epigrafe() {
}
public Epigrafe(String id) {
this.id=id;
}

private void setId(String id) {
this.id = id;
}

public void setEjercicio(Ejercicio ejercicio) {
this.ejercicio = ejercicio;
}

public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}

public void setFormula(String formula) {
this.formula = formula;
}

public void setEpigrafePadre(Epigrafe epigrafePadre) {
this.epigrafePadre = epigrafePadre;
}

private void setEpigrafesHijos(Set<Epigrafe> epigrafesHijos) {
this.epigrafesHijos = epigrafesHijos;
}

private void setCuentasHijas(Set<Cuenta> cuentasHijas) {
this.cuentasHijas = cuentasHijas;
}

public void addEpigrafe(Epigrafe epigrafe) {
if(epigrafe.getEpigrafePadre()!=null)
epigrafe.getEpigrafePadre().removeEpigrafe(epigrafe);
epigrafe.setEpigrafePadre(this);
epigrafesHijos.add(epigrafe);
}

public void removeEpigrafe(Epigrafe epigrafe) {
epigrafe.setEpigrafePadre(null);
epigrafesHijos.remove(epigrafe);
}

public void addCuenta(Cuenta cuenta) {
if(cuenta.getEpigrafe()!=null)
cuenta.getEpigrafe().removeCuenta(cuenta);
cuenta.setEpigrafe(this);
cuentasHijas.add(cuenta);
}

public void removeCuenta(Cuenta cuenta) {
cuenta.setEpigrafe(null);
cuentasHijas.remove(cuenta);
}


public String getId() {
return id;
}

public Ejercicio getEjercicio() {
return ejercicio;
}

public String getDescripcion() {
return descripcion;
}

public String getFormula() {
return formula;
}

public Epigrafe getEpigrafePadre() {
return epigrafePadre;
}

public Set<Epigrafe> getEpigrafesHijos() {
return epigrafesHijos;
}

public Set<Cuenta> getCuentasHijas() {
return cuentasHijas;
}
}


Full stack trace of any exception that occurs:
java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:682)

at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:494)

at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)

at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:215)

at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:373)

at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:281)

at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:640)

at org.hibernate.proxy.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:94)

at org.hibernate.proxy.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:42)

at org.hibernate.tuple.PojoTuplizer.buildProxyFactory(PojoTuplizer.java:144)

at org.hibernate.tuple.AbstractTuplizer.<init>(AbstractTuplizer.java:83)

at org.hibernate.tuple.PojoTuplizer.<init>(PojoTuplizer.java:54)

at org.hibernate.tuple.TuplizerLookup.create(TuplizerLookup.java:47)

at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:218)

at org.hibernate.persister.entity.BasicEntityPersister.<init>(BasicEntityPersister.java:400)

at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:104)

at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)

at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:211)

at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1005)

at bulldog.server.BulldogServer.initHibernate(BulldogServer.java:285)

at bulldog.server.BulldogServer.<init>(BulldogServer.java:55)

at bulldog.server.Server.startBulldog(Server.java:50)

at bulldog.server.Server.start(Server.java:32)

at bulldog.server.gui.ServerManager.jInitButton_actionPerformed(ServerManager.java:95)

at bulldog.server.gui.ServerManager$2.actionPerformed(ServerManager.java:68)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:269)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)

at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)

at java.awt.Component.processMouseEvent(Component.java:5488)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)

at java.awt.Component.processEvent(Component.java:5253)

at java.awt.Container.processEvent(Container.java:1966)

at java.awt.Component.dispatchEventImpl(Component.java:3955)

at java.awt.Container.dispatchEventImpl(Container.java:2024)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)

at java.awt.Container.dispatchEventImpl(Container.java:2010)

at java.awt.Window.dispatchEventImpl(Window.java:1766)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

20:30:54,546 WARN PojoTuplizer:156 - could not create proxy factory for:bulldog.compta.domainobj.Epigrafe

org.hibernate.HibernateException: CGLIB Enhancement failed: bulldog.compta.domainobj.Epigrafe

at org.hibernate.proxy.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:108)

at org.hibernate.proxy.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:42)

at org.hibernate.tuple.PojoTuplizer.buildProxyFactory(PojoTuplizer.java:144)

at org.hibernate.tuple.AbstractTuplizer.<init>(AbstractTuplizer.java:83)

at org.hibernate.tuple.PojoTuplizer.<init>(PojoTuplizer.java:54)

at org.hibernate.tuple.TuplizerLookup.create(TuplizerLookup.java:47)

at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:218)

at org.hibernate.persister.entity.BasicEntityPersister.<init>(BasicEntityPersister.java:400)

at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:104)

at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)

at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:211)

at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1005)

at bulldog.server.BulldogServer.initHibernate(BulldogServer.java:285)

at bulldog.server.BulldogServer.<init>(BulldogServer.java:55)

at bulldog.server.Server.startBulldog(Server.java:50)

at bulldog.server.Server.start(Server.java:32)

at bulldog.server.gui.ServerManager.jInitButton_actionPerformed(ServerManager.java:95)

at bulldog.server.gui.ServerManager$2.actionPerformed(ServerManager.java:68)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:269)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)

at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)

at java.awt.Component.processMouseEvent(Component.java:5488)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)

at java.awt.Component.processEvent(Component.java:5253)

at java.awt.Container.processEvent(Container.java:1966)

at java.awt.Component.dispatchEventImpl(Component.java:3955)

at java.awt.Container.dispatchEventImpl(Container.java:2024)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)

at java.awt.Container.dispatchEventImpl(Container.java:2010)

at java.awt.Window.dispatchEventImpl(Window.java:1766)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Caused by: java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:682)

at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:494)

at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)

at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:215)

at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:373)

at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:281)

at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:640)

at org.hibernate.proxy.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:94)

... 42 more


Name and version of the database you are using:
Derby


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 4:28 pm 
Regular
Regular

Joined: Wed May 11, 2005 11:57 pm
Posts: 80
I believe that the docs state that it can be package private, not private. So

Code:
public class MyClass {
    MyClass() {}
}


Should work, although this will not:

Code:
public class MyClass {
    private MyClass() {}
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 4:39 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
From the documentation:

Quote:
The no-argument constructor is a requirement for all persistent classes; Hibernate has to create objects for you, using Java Reflection. The constructor can be private, however, package visibility is required for runtime proxy generation and efficient data retrieval without bytecode instrumentation.

http://www.hibernate.org/hib_docs/v3/re ... firstclass

See also:
http://www.hibernate.org/hib_docs/v3/re ... onstructor

Quote:
Cat has a no-argument constructor. All persistent classes must have a default constructor (which may be non-public) so Hibernate can instantiate them using Constructor.newInstance(). We recommend having a constructor with at least package visibility for runtime proxy generation in Hibernate.


Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject: way to violate the business object's contract in hibernate
PostPosted: Wed Mar 08, 2006 3:18 am 
Newbie

Joined: Mon Jun 27, 2005 4:22 am
Posts: 10
Location: Hyderabad, India
The documentation is fine. But i see the following problem in hibernate.

"Giving a way to violate the business object's contract"

Let me explain with an example.

we have a business object with some properties which should not be null at any point in time.

Those properies not null constraint is the only class invariant.

Now as per hibernate documentation, if we provide the package visibility default constructor, lets see what will happen.


package testvisibility;

public class MyBusinessObject{

/* @pre not null */
private BusinessDataType propertyOne;

* @pre not null */
private BusinessDataType propertyTwo;

MyBusinessObject() {
}

public BusinessReturnType methodOne(){
// does something to return the business return type.
}

public boolean equals(Object theObject) {
// here i dont want to check for not null constrains, as my properties are by contract not-null
}

}


Now this way anybody can expose the constructor in MyBusinessObject which we dont want to do, according to our
business contracts.


package testvisibility;

public class MyAnotherBusinessObject{

public MyAnotherBusinessObject() {
// anybody can call this as this object is also written in the same package, even if the other class written and packaged in another jar.
super();
}

public static void main(String args[]){
MyBusinessObject aMyBusinessObject = new MyAnotherBusinessObject();
if (aMyBusinessObject.equals(someOtherObect)){ // this line will throw null pointer exception as the properties in MyBusinessObject are null now.

}
}

}


I hope hibernate will consider this basic feature of "Not giving a way to violate the business object's contract", which by business is mandatory and the framework should not violate that.


regards,
Amjath


Top
 Profile  
 
 Post subject: Use interceptor
PostPosted: Wed Mar 08, 2006 10:07 am 
Regular
Regular

Joined: Wed May 11, 2005 11:57 pm
Posts: 80
Another alternative way to accomplish this is by using a Session Interceptor. This will let you override the default Hibernate behavior (calling the no-arg constructor) with one of your own choosing. This is not a perfect solution, because whatever constructor you create needs to be at least package-private. This means that another business object could call your constructor no matter what method signature you use, but at least you could use something less obvious than a no-arg constructor.

We created a Hibernate-specific constructor in all of our business objects that takes a Serializable id attribute as well as an unused byte parameter (just to reduce the likelihood of accidentally calling the Hibernate constructor), and that seems to work well.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 11:10 am 
Newbie

Joined: Mon Jun 27, 2005 4:22 am
Posts: 10
Location: Hyderabad, India
Thanks for the reply. But there is some small error in the code i have mentioned in my earlier post.

The error is, i forgot to extend the MyBusinessDataType class in the 2nd class


And the next thing is, I dont mind providing even a public constructor which takes the 2 properties which are not-null as that is the only way the users of my class can construct the object of it, maintaining the class invariants intact.

I want solution for that in hibernate.

Your post though it gives me some other options which is good when i dont have any other best option in hand, hibernate should make sure its not giving any way to the users of the class to violate the contract.

i hope this makes sense.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 11:12 am 
Newbie

Joined: Mon Jun 27, 2005 4:22 am
Posts: 10
Location: Hyderabad, India
Oopsssssss...once again i made error in the class name.

Sorry for that.

The class name was, MyBusinessObject

hope i dont repeat these kind of mistakes in the public forum again....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.