留言板

Service-builder: customize id generator

Serguei Carrascosa Cervilla,修改在6 年前。

Service-builder: customize id generator

New Member 帖子: 6 加入日期: 16-3-18 最近的帖子
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,修改在6 年前。

RE: Service-builder: customize id generator (答复)

Liferay Legend 帖子: 14916 加入日期: 06-9-2 最近的帖子
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,修改在6 年前。

RE: Service-builder: customize id generator

New Member 帖子: 6 加入日期: 16-3-18 最近的帖子
It works!

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

Thanks.