Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » EclipseLink with JSR 303 - Bean Validation in an RCP environment
EclipseLink with JSR 303 - Bean Validation in an RCP environment [message #676014] Thu, 02 June 2011 13:48 Go to next message
Andreas Fagschlunger is currently offline Andreas Fagschlunger
Messages: 12
Registered: June 2011
Junior Member
Hy all!

I am new to RCP devolpment and right now I am working on a simple RCP application with UI and DB (embedded Derby) support.

The last two days I worked on setting up JPA 2.0 with EclipseLink. It finally worked - the EclipseLink Comics example was very helpfull.

But now I am stuck on getting JSR 303 - Bean Validation to work with EclipseLink and Hibernate Validator.

I have created two Plugins from the JARs hibernate-validator-4.1.0.Final.jar and validation-api-1.0.0.GA.jar. I added both Plugins as dependencies two my Plugin. But the EclipseLink Log shows the following line:

[EL Finer]: Could not initialize Validation Factory. Encountered following exception: javax.validation.ValidationException: Unable to find a default provider

The same Exception occurs if I try to get a ValidationFactory outside of EclipseLink (Validation.buildDefaultValidatorFactory()).

I also tried the approach described here:

katastrophos.net/magnus/blog/2009/07/18/having-fun-with-jsr-303-beans-validation-and-osgi-spring-dm/

But then I get a ClassNotFoundException:

Caused by: java.lang.ClassNotFoundException: javax.validation.spi.ValidationProvider
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:506)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:422)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:410)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 63 more

So is there a way to get Bean Validation work within a RCP application? Best with EclipseLink doing the Validation-Check?

Best regards,
Billie
Re: EclipseLink with JSR 303 - Bean Validation in an RCP environment [message #676029 is a reply to message #676014] Thu, 02 June 2011 14:56 Go to previous messageGo to next message
Shaun Smith is currently offline Shaun Smith
Messages: 197
Registered: July 2009
Senior Member
Hi Billie,

I think the problem is that the classloader EclipseLink is using for your persistence unit cannot see the Validator classes. When you create your EntityManagerFactory you can pass a classloader that includes the Validator bundle. This could be achieved by either ensuring the classloader you're passing is from a bundle that has visibility to the Validator classes or by building a CompositeClassLoader that includes both your persistence unit classes/resources and the Validator bundle classloader. The first approach is easiest. The second can be done using some internal classes in org.eclipse.persistence.internal.jpa.deployment.osgi: BundleProxyClassLoader and CompositeClassLoader. Those classes should probably not be internal.

--Shaun

Re: EclipseLink with JSR 303 - Bean Validation in an RCP environment [message #676072 is a reply to message #676029] Thu, 02 June 2011 17:37 Go to previous messageGo to next message
Andreas Fagschlunger is currently offline Andreas Fagschlunger
Messages: 12
Registered: June 2011
Junior Member
Hello Shaun!

I tried again and now it seems Bean Validation runs outside of EclipseLink. I've created one Plugin from the following JARs:

hibernate-validator-4.1.0.Final.jar
validation-api-1.0.0.GA.jar
slf4j-log4j12-1.5.6.jar
slf4j-api-1.5.6.jar
log4j-1.2.14.jar

So I replaced the two Plugins by this one. My Plugin depends on this "org.hibernate.validator"-Plugin.

The ClassLoader i initalize the EntityManagerFactory with, is the ClassLoader from my Plugin Activator-Class.

See the following code:

public class CoreActivator implements BundleActivator ...

	private EntityManagerFactory getEntityManagerFactory() {
		if (entityManagerFactory == null) {
			ClassLoader cl = getClass().getClassLoader();
			try {
				ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
				Validator v = vf.getValidator();
				Writer model = new Writer();
				Set<ConstraintViolation<Writer>> violations = v.validate(model);
				System.out.println("violations.size()" + violations.size());
			} catch (Exception e) {
				e.printStackTrace();
			}
			Map<Object, Object> properties = new HashMap<Object, Object>();
			properties.put(PersistenceUnitProperties.CLASSLOADER, cl);
			entityManagerFactory = new PersistenceProvider()
					.createEntityManagerFactory("library", properties);
		}
		return entityManagerFactory;
	}


The validation code runs through without Exceptions and I have logs from org.hibernate.validator.

But EclipseLink now outputs the following log entry:

[EL Finer]: Could not initialize Validation Factory. Encountered following exception: java.lang.NoClassDefFoundError: javax/validation/Validation


I don't now if this error is better or worse. The ClassLoader (cl) finds the Validation-Class. I don't understand why EclipseLink does not find the class.

Can you give me a hint?
Re: EclipseLink with JSR 303 - Bean Validation in an RCP environment [message #676074 is a reply to message #676072] Thu, 02 June 2011 17:44 Go to previous message
Andreas Fagschlunger is currently offline Andreas Fagschlunger
Messages: 12
Registered: June 2011
Junior Member
Finally made it, just have to Reexport the dependency of my "org.hibernate.validator"-Plugin in my own Plugin.

Not often so happy about seeing a Exception, but this time i am:

javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'. Please refer to embedded ConstraintViolations for details.
	at org.eclipse.persistence.internal.jpa.metadata.listeners.BeanValidationListener.validateOnCallbackEvent(BeanValidationListener.java:90)
	at org.eclipse.persistence.internal.jpa.metadata.listeners.BeanValidationListener.prePersist(BeanValidationListener.java:62)
	at org.eclipse.persistence.descriptors.DescriptorEventManager.notifyListener(DescriptorEventManager.java:698)
	at org.eclipse.persistence.descriptors.DescriptorEventManager.notifyEJB30Listeners(DescriptorEventManager.java:641)
	at org.eclipse.persistence.descriptors.DescriptorEventManager.executeEvent(DescriptorEventManager.java:200)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectClone(UnitOfWorkImpl.java:4216)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNotRegisteredNewObjectForPersist(UnitOfWorkImpl.java:4193)
	at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.registerNotRegisteredNewObjectForPersist(RepeatableWriteUnitOfWork.java:493)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4135)
	at [b]org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:406)[/b]
Previous Topic:how to change default schema - postgresql
Next Topic:(no subject)
Goto Forum:
  


Current Time: Fri Oct 31 10:09:56 GMT 2014

Powered by FUDForum. Page generated in 0.14412 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software