Formula cannot be cast to class org.hibernate.mapping.Column

Viewed 428

I'm trying to write a @ManyToOne and @OnoToMany relation for old database tables. These tables do not have a dedicated foreign key and primary key id column but use two columns to join data.

How can I join customer ids (concatenating id1 and id2) with access`s userId? I tried @JoinColumnsOrFormulas also, but it fails.

@Entity
public class Access {

   private String userId;
      

   @ManyToOne
//    @JoinColumnsOrFormulas({
//            @JoinColumnOrFormula(column = @JoinColumn(name = "user_id")),
//            @JoinColumnOrFormula(formula = @JoinFormula("concat(id1, id2)"))
//    })
   @JoinFormula("(SELECT c.id FROM Customer c WHERE c.id1 || c.id2 = userId)")
   private Customer customer;
}
@Entity
public class Customer {

 private Long id;

 private String id1;

​ private String id12;
 
 @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
 private Set<Access> accesses;

When the application starts it throws this exception:

Caused by: java.lang.ClassCastException: class org.hibernate.mapping.Formula cannot be cast to class org.hibernate.mapping.Column (org.hibernate.mapping.Formula and org.hibernate.mapping.Column are in unnamed module of loader 'app')

Thank in advance

0 Answers
Related