@Convert to @Id field
I'm trying to
@Id
@Column(name = "MY_ID_FIELD")
@Convert(converter = IdConverter.class)
private Long id;
IdConverter:
@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 render BigDecimal, the Hibernate attribute field type expects in a SQL Server database id column type to be numeric, up to Long.
I am using Spring Data Jpa and my repository is using Long as I would expect
@Repository
public interface CityRepository extends JpaRepository<City, Long> { }
Do you have any idea why it doesn't work?
+3
source to share
1 answer
@Id
and @Convert
cannot be used together. Using @IdClass
can fix this problem. You just need to move @Convert
to @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;
}
+2
source to share