0
votes

I am trying to define a JPA @Converter for Joda-Time which gets put in an EJB JAR along with the result of the JPA @Entity and a @Stateless session bean like this:

@Converter(autoApply = true)
public class LocalDateConverter implements
        AttributeConverter<LocalDate, String> {

    @Override
    public String convertToDatabaseColumn(final LocalDate localDate) {
        if (localDate == null) {
            return null;
        }
        return localDate.toString();
    }

    @Override
    public LocalDate convertToEntityAttribute(final String dbData) {
        if (dbData == null) {
            return null;
        }
        return LocalDate.parse(dbData);
    }
}

I have a session bean that looks like:

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class VenueTableModule {

    @PersistenceContext
    EntityManager em;

    public Bar foo() {
        final Bar bar = new Bar ();
        bar.name = "foo" + UUID.randomUUID();
        bar.startDate = LocalDate.now();
        em.persist(bar);
        em.flush();
        return bar;
    }
}

which fails on the em.persist(bar) line when I invoke it from the servlet. The code works without the em.* lines I don't get the error when the converter, stateless session beans, etc. are moved to the web application which I know is allowed and I may have to do it. So it appears that the @Converter is using a different class loader than the rest of the EJB jar.

I am using Glassfish 4.0, and I am not sure if it is a bug GLASSFISH-21161 or if I am just doing something incorrectly. However, the code I have seems to work with WildFly

1
If you could have posted the stacktrace, it would help check exactly what is the problemmaress
Added the stack trace to the bug reportArchimedes Trajano

1 Answers

0
votes

The AttributeConverter is a Generic type class. See the Java Doc below:

/**
 * A class that implements this interface can be used to convert 
 * entity attribute state into database column representation 
 * and back again.
 * Note that the X and Y types may be the same Java type.
 *
 * @param <X>  the type of the entity attribute
 * @param <Y>  the type of the database column
 */
public interface AttributeConverter<X,Y> 

Try changing your LocalDateConverter to

public class LocalDateConverter implements AttributeConverter<LocalDate, String> 

where LocalDate is the type of the attribute in your entity bean and String is the type the database expects as a varchar or char or text field.