-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support @Convert specification on id attributes #207
Comments
There are two separate things here.
They are distinct, so you should separate this issue out into 2. |
Hi @andyjefferson , In fact, Hibernate 5 fails in any case. Anyway, I will create a separate issue for the generated value case. |
@andyjefferson @reda-alaoui Do we have any support for this now?. I see the issue is quite old. |
@andyjefferson isn’t the current issue opened on the jakarta jpa specification project? |
Similar to #208. |
In order to write clean and readable code, I try to use wrapped Id object and avoid primitive String/Integer Id, but as reda-alaoui said, the code below doesn't work. @Entity
class ExampleEntity {
@Id
@Column(name= "fd_id")
@Convert(converter = ExampleIdConverter.class)
private ExampleId id;
@Value(staticConstructor = "of")
public static class ExampleId implements Serializable {
String idAsString;
}
} I'm currently using @EmbeddedId to achieve wrapped Id. @Entity
@Table(name = "example_table")
public class ExampleEntity {
@EmbeddedId
@AttributeOverride(name = "idAsString", column = @Column(name = "fd_id"))
private ExampleId id;
@Embeddable
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@EqualsAndHashCode
@AllArgsConstructor(staticName = "of")
public static class ExampleId implements Serializable {
private String idAsString;
}
} But I think this is less easy to use than the @ Convert & AttributeConverter proposal.
Hi @reda-alaoui , as of now, do you know of any viable ways for implementing single-attribute wrapper ID? Looking forward to hearing about others' experiences with this. |
@BearKid , I have been using primitive id since. I don't know how things evolved in between. |
As far as I know, no product implements this (since it's actually pretty hard to implement) so it's not something we plan to add to the spec at this time. |
I wanted to point out that in Hibernate 5.6.15.Final it is possible to use @convert on a @id attribute. I have been doing that for quite some time, exactly to map immutable wrappers to my ids. Now that I'm upgrading Hibernate to version 6.4 is that I found out that it no longer works and was "forbidden" by the JPA specs. |
If you think you found a bug or want to request a feature in Hibernate ORM, please do that through the Hibernate ORM community channels. This is not the proper place for that sort of discussion. |
Revisiting my code in order to see if I can remove a Hibernate Type that is a duplicate implementation of another JPA converter, and find that JPA still doesn't allow Convert on Id fields. My use case is for accessing legacy tables that use CHAR(N) columns as PK, the idea is to strip the ending spaces of the Id at read time and avoid needing to strip the id on every usage site. @gavinking, You are right that no other implementation allows Convert on Id fields, but Hibernate allows Type on Id fields without any problem. |
Correct, and the reason is because a |
We want to map an id attribute to an immutable type.
Hibernate 5 fails to convert the id to the immutable type.
Reading the JPA 2.1 spec, we found this:
Would it be possible to reconsider this to make
convert on id attributes
a use case part of the next version of JPA?The text was updated successfully, but these errors were encountered: