15

I'm trying to

@Id
@Column(name = "MY_ID_FIELD")
@Convert(converter = IdConverter.class)
private Long id;

IdConverter is:

@Converter
public class IdConverter implements AttributeConverter<Long, BigDecimal> {

    @Override
    public BigDecimal convertToDatabaseColumn(Long attribute) {
        return BigDecimal.valueOf(attribute);
    }

    @Override
    public Long convertToEntityAttribute(BigDecimal dbData) {
        return dbData.longValue();
    }
}

The converter will map BigDecimal, the attribute field type Hibernate expects given that in the sql server database the id column type is numeric, to Long.

I'm using Spring Data Jpa and my repository is using Long as I would expect to

@Repository
public interface CityRepository extends JpaRepository<City, Long> { }

Do you have any idea of why it is not working?

6
  • I did not understand your problem. Are you generating any exceptions?
    – Pr3ds
    May 19, 2017 at 12:34
  • The JPA spec says something about @Convert not being applied to @Id fields. I know that DataNucleus JPA allows it, but have also heard of others that don't.
    – user3973283
    May 19, 2017 at 12:34
  • @Ulisses, my problem is that the application does not start because of the data type of my id field, Long, Hibernate is expecting BigDecimal, as if there were no converter at all. And thanks Billy I'll take a look at the docs then.
    – johncol
    May 19, 2017 at 12:44
  • The @BillyFrost is correct, according to the documentation: The Convert annotation should not be used to specify conversion of the following: Id attributes, version attributes, relationship attributes, and attributes explicitly denoted as Enumerated or Temporal. Applications that specify such conversions will not be portable.
    – Pr3ds
    May 19, 2017 at 13:02
  • I understand you're trying to map an existing database. Since NUMERIC is a floating-type number, I don't think there is much you could do (other than maybe changing the DB to use BIGINT instead). If you're creating the database from scratch, then you are most likely trying to use the wrong org.hibernate.dialect.Dialect for your DB
    – crizzis
    May 20, 2017 at 19:33

3 Answers 3

23

@Id and @Convert could not be used together. Using @IdClass can fix this problem. You just need to move @Convert to the @IdClass.

@Entity
@IdClass(PK.class)
public class YourClass {
  @Id
  private Long id;
  ...
}


public class PK implements Serializable {
  @Column(name = "MY_ID_FIELD")
  @Convert(converter = IdConverter.class)
  private Long id;
}
4
  • 2
    This is incorrect. Even using the @IdClass does not make it work (I tried it).
    – David H
    Dec 10, 2017 at 2:27
  • It is really work. May be you lost something. I make a simple unit test to prove it. You can check it by the unit test class IdWithConverterTest.java. You can also check it through the console log. To run this unit test, 1) $ git clone --branch java-jpa https://github.com/rj-hwang/beginner.git java-jpa.2) $ cd java-jpa. 3) $ mvn test -D test=com.beginner.jpa.idwithconverter.IdWithConverterTest
    – RJ.Hwang
    Dec 12, 2017 at 1:53
  • I confirm it works. I just tried on an Enum @Id member. Sep 19, 2019 at 9:07
  • If using @Column on the entity class, it's will be does't work, because cannot using @Id with @Convert together, we must move the @Column from entity class into id class and stay @Id on entity class alone.
    – Alex
    Apr 17, 2020 at 9:58
10

In case anyone else runs into the same problem I did, you have to have the @Column annotation on the @IdClass in addition to @Converter for it to work (instead of relying on the naming strategy).

// doesn't work
public class PK implements Serializable {
  @Convert(converter = IdConverter.class)
  private Long id;
}
// works
public class PK implements Serializable {
  @Column(name = "ID")
  @Convert(converter = IdConverter.class)
  private Long id;
}
0

With EclipseLink 2.7.6 I've got the same problem.
My solution is:

Entity class:


    @Entity(name = "xxx")
    public class xxx implements Serializable {
      @EmbeddedId
      private XxxPk pk;
      ...
    }

ID class:


    @Embeddable
    public class XxxPk implements Serializable {
      @Column(name = "A_TYPE")
      @Convert(converter = ATypeAttributeConverter.class)
      private AType aTypeAttribute;
      ...
    }

Converter:


    import javax.persistence.AttributeConverter;
    import javax.persistence.Converter;
        
    @Converter
    public class ATypeAttributeConverter implements AttributeConverter<AType , String> {
        
      @Override
      public String convertToDatabaseColumn(AType agrType) { ... }
        
      @Override
      public AType convertToEntityAttribute(String code) { ... }
    }

Enumeration:


    public enum AType {
      E1, E2, ...;
      ...
    }

I've checked it also with EclipseLink-Converter (org.eclipse.persistence.annotations.Converter), but without success.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.