1

We have a class Phone which uses Roles, ie.

@Entity
public class Phone{
  @Id...
  private Integer id;

  @Column(name="role_id")
  private IRole role;
  ...

and the interface

public interface IRole extends Serializable {
    public abstract Integer getId();
    ...

So when I try to persist Phone in my package the IRole isn't any longer abstract it is already a concrete entity. The reason why I cannot use the concrete class directly is that we implemented the Role differently in projects.

When I try to start Hibernate I get an Exception:

org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [role_id
] in table [phone]; found [int4 (Types#INTEGER)], but expecting [bytea (Types#VARBINARY)]

I think the IRole causes the problem. It should store the id of the Role - which is available in all our implementations. How to tell Hibernate that it should use type Integer instead of some interface interpolation?

Looking for a solution which would work in Postgres as well in MariaDB.

LeO
  • 4,238
  • 4
  • 48
  • 88
  • Change `role` from `IRole` to `Integer` in your `Phone` class and then fix the references. – locus2k Dec 05 '19 at 16:43
  • Yeah, but I want to keep integrity of the objects and not start handling my own objects. – LeO Dec 05 '19 at 16:47
  • Maybe this will help: https://stackoverflow.com/questions/3827494/inherited-abstract-class-with-jpa-hibernate. Its for abstract class not for interfaces though – locus2k Dec 05 '19 at 16:53

1 Answers1

1

You can implement an attribute converter for each your particular implementation of IRole interface and then use them.

  @Convert(converter=RoleA.class)
  @Column(name="role_id_a")
  private IRole roleA;

  @Convert(converter=RoleB.class)
  @Column(name="role_id_b")
  private IRole roleB;
SternK
  • 11,649
  • 22
  • 32
  • 46