Foren

Service-builder: customize id generator

Serguei Carrascosa Cervilla, geändert vor 6 Jahren.

Service-builder: customize id generator

New Member Beiträge: 6 Beitrittsdatum: 18.03.16 Neueste Beiträge
Hi,

I'm trying to implement a custom primary key generator with de service-builder in Liferay 6.2. The key must match the pattern "VAC1, VAC2, VAC3,...". According to the definition of service-builder, service.xml would be:


<column name="id" type="String" primary="true" id-type="class" id-param="com.liferay.counter.service.persistence.VACGenerator" />


And VACGenerator.java:


public class VACGenerator implements IdentifierGenerator {
	
	private final static String COD_CERT_PREFIX = "VAC";
	
	@Override
	public Serializable generate(SessionImplementor arg0, Object arg1) throws HibernateException {
		
		Certificacion certificacion = CertificacionLocalServiceUtil.getLast();
		String codiCertificacio = COD_CERT_PREFIX + 1;
		if (certificacion != null) {
			String codi = certificacion.getCodigoCertificacion();
			int numCertif = Integer.parseInt(codi.substring(3));
			codiCertificacio = COD_CERT_PREFIX + ++numCertif;
		}
		return codiCertificacio;
	}
}


After build service and deploy, get this error message:
Caused by: org.hibernate.MappingException: Could not interpret id generator strategy [com.fihoca.service.util.VACGenerator]
	at org.hibernate.id.factory.DefaultIdentifierGeneratorFactory.getIdentifierGeneratorClass(DefaultIdentifierGeneratorFactory.java:137)
	at org.hibernate.id.factory.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:108)
	... 45 more


Has anyone successfully implemented a custom key generator?

Thanks.
thumbnail
David H Nebinger, geändert vor 6 Jahren.

RE: Service-builder: customize id generator (Antwort)

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
You may be running into a class loader issue. Try putting your class file to ROOT/WEB-INF/classes/... and see if that fixes it.

Note this would just be a short-term fix. If it works, you would normally deploy this using an EXT plugin; this is just a quick way to test if you're hitting a class loader issue or not.










Come meet me at Devcon 2017 or 2017 LSNA!
Serguei Carrascosa Cervilla, geändert vor 6 Jahren.

RE: Service-builder: customize id generator

New Member Beiträge: 6 Beitrittsdatum: 18.03.16 Neueste Beiträge
It works!

Putting VACGenerator.class file into ROOT/WEB-INF/classes/.../ and restat server. Next, I'll try to deploy a EXT-plugin.

Thanks.