Vraptor + JBoss AS 7.1 + JPA - Erro de Joda time [RESOLVIDO]

11 respostas
vasilvei

Bom tarde estou usando o Vraptor+jboss7.1, JPA, para a conversão de data estou usando o Jodatime

nas minhas classes javas que tem data eu faço o seguinte:

...
private LocalDate dataagenda;
....

@Type(type = "org.joda.time.contrib.hibernate.PersistentLocalDate")
	@Column(name = "dataagenda", length = 13)
	public LocalDate getDataagenda() {
		return this.dataagenda;
	}

	public void setDataagenda(LocalDate dataagenda) {
		this.dataagenda = dataagenda;
	}

e nos JSP:

<%@ taglib uri="http://www.joda.org/joda/time/tags" prefix="joda"%>

.....

 <input id="dataagenda" class="limpar inputForm datepicker"  type="text" 
     alt="date" name="agendamento.dataagenda" size="10" maxlength="10"  value="<joda:format pattern="dd/MM/yyyy" value="${agendamento.dataagenda}"/>">

quando eu usava tomcat funcionava de boa, agora que estou usando o jboss, ele conecta com o banco e tudo certo, mas quando chamo um objeto que tem LocaDate da essa exception:

Unhandled exception occurred whilst decorating page: br.com.caelum.vraptor.InterceptionException: exception raised, check root cause for details: java.lang.AbstractMethodError: org.joda.time.contrib.hibernate.PersistentLocalDate.nullSafeGet(Ljava/sql/ResultSet;[Ljava/lang/String;Lorg/hibernate/engine/spi/SessionImplementor;Ljava/lang/Object;)Ljava/lang/Object;

eu tentei colocar os jar do jodatime dentro da pasta /HOMEJBOSS/modules/org/joda/time/main, mas da o mesmo erro…

Alguém sabe como posso resolver isso? Obrigado!

11 Respostas

Lucas_Cavalcanti

é conflito de versão do joda-time-hibernate com a versão do hibernate que vc tá usando… dá uma olhada nas docs do jodatime-hibernate pra ver qual é a versão compatível com a que vc tá usando.

vasilvei

O jboss usa a versão do hibernate 4.0.1, e não compativel com a joda-time-hibernate-1.3,
como eu poderia revolver isso eu tenho que usar a jboss.

você teria alguma sujestão para mim.

Lucas_Cavalcanti

isso é uma pergunta? :wink:

se não existir uma versão mais nova do joda-time-hibernate que é compatível com o hibernate 4.0.1, vc sempre pode criar o seu próprio UserType do hibernate e fazer funcionar…

basicamente é copiar o do jodatime e ajustar pra api nova.

vasilvei

Ok!

eu criei a class PersistentLocalDate

package br.com.coliseucrm.util;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.usertype.EnhancedUserType;
import org.joda.time.LocalDate;

/**
 * Persist {@link org.joda.time.LocalDate} via hibernate.
 * 
 * @author Mario Ivankovits ([email removido])
 */
public class PersistentLocalDate implements EnhancedUserType, Serializable {

	public static final PersistentLocalDate INSTANCE = new PersistentLocalDate();

	private static final int[] SQL_TYPES = new int[] { Types.DATE, };

	public int[] sqlTypes() {
		return SQL_TYPES;
	}

	public Class returnedClass() {
		return LocalDate.class;
	}

	public boolean equals(Object x, Object y) throws HibernateException {
		if (x == y) {
			return true;
		}
		if (x == null || y == null) {
			return false;
		}
		LocalDate dtx = (LocalDate) x;
		LocalDate dty = (LocalDate) y;
		return dtx.equals(dty);
	}

	public int hashCode(Object object) throws HibernateException {
		return object.hashCode();
	}

	public Object nullSafeGet(ResultSet resultSet, String[] strings,
			Object object) throws HibernateException, SQLException {
		return nullSafeGet(resultSet, strings[0]);

	}

	public Object nullSafeGet(ResultSet resultSet, String string)
			throws SQLException {
		Object timestamp = StandardBasicTypes.DATE.nullSafeGet(resultSet,
				string);
		if (timestamp == null) {
			return null;
		}
		return new LocalDate(timestamp);
	}

	public void nullSafeSet(PreparedStatement preparedStatement, Object value,
			int index) throws HibernateException, SQLException {
		if (value == null) {
			StandardBasicTypes.DATE.nullSafeSet(preparedStatement, null, index);
		} else {
			StandardBasicTypes.DATE.nullSafeSet(preparedStatement,
					((LocalDate) value).toDateTimeAtStartOfDay().toDate(),
					index);
		}
	}

	public Object deepCopy(Object value) throws HibernateException {
		return value;
	}

	public boolean isMutable() {
		return false;
	}

	public Serializable disassemble(Object value) throws HibernateException {
		return (Serializable) value;
	}

	public Object assemble(Serializable cached, Object value)
			throws HibernateException {
		return cached;
	}

	public Object replace(Object original, Object target, Object owner)
			throws HibernateException {
		return original;
	}

	public String objectToSQLString(Object object) {
		throw new UnsupportedOperationException();
	}

	public String toXMLString(Object object) {
		return object.toString();
	}

	public Object fromXMLString(String string) {
		return new LocalDate(string);
	}

}

daí no caso onde eu tenho isso:

@Type(type = "org.joda.time.contrib.hibernate.PersistentLocalDate")

trocaria por isso:

@Type(type = "br.com.coliseucrm.util.PersistentLocalDate")

Tem essa parte que o nullSafeGet, que esta deprecated, você saberia por qual eu deveria substituir?

Object timestamp = StandardBasicTypes.DATE.nullSafeGet(resultSet,
				string);
Lucas_Cavalcanti

geralmente está no javadoc…

vasilvei

Lucas pesquisando eu cheguei a achar esse link :

e baixei o jar http://grepcode.com/snapshot/repo1.maven.org/maven2/org.jadira.usertype/usertype.jodatime/2.0/

e coloquei no meu projeto:

ajustei as classes:

@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
	@Column(name = "dataagenda", length = 13)
	public LocalDate getDataagenda() {
		return this.dataagenda;
	}

	public void setDataagenda(LocalDate dataagenda) {
		this.dataagenda = dataagenda;
	}

e esta dando essa excetion

(MSC service thread 1-5) Failed to define class org.jadira.usertype.dateandtime.joda.PersistentLocalDate in Module "deployment.Coliseucrm.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link org/jadira/usertype/dateandtime/joda/PersistentLocalDate (Module "deployment.Coliseucrm.war:main" from Service Module Loader)

Could not determine type for: org.jadira.usertype.dateandtime.joda.PersistentLocalDate, at table: atendimento, for columns: [org.hibernate.mapping.Column(dataocorrencia)]

saberia me dizer porque não estaria dando certo?

Obrigado!

Lucas_Cavalcanti

o jar desse jodatime novo foi pro WAR? ou pelo menos pra WEB-INF/lib?

vasilvei

Eu coloquei ele dentro da pasta WEB-INF/lib, e fui em buid path -> add class path.

Lucas_Cavalcanti

vc copiou o nome completo da classe corretamente?

vasilvei

Deu certo Lucas, estava faltando o .jar usertype.spi-2.0.jar, neste jar esta a class: AbstractSingleColumnUserType, que é herdada por PersistentLocalDate.

links se alguém tiver o mesmo problema que eu:

http://grepcode.com/snapshot/repo1.maven.org/maven2/org.jadira.usertype/usertype.jodatime/2.0/
http://grepcode.com/snapshot/repo1.maven.org/maven2/org.jadira.usertype/usertype.spi/2.0/

Obrigado!

ThiagoInGuj

Valeu mesmo vasilvei!

Criado 4 de maio de 2012
Ultima resposta 25 de jun. de 2012
Respostas 11
Participantes 3