vlad wrote:
It works just fine for me. Check out this test on
GitHub. I'm using Hibernate 5.2.10.
Are you sure you added the @JoinFormula to the @MantyToOne? From your error message it looks like you added it to some basic attribute or the id.
Quote:
After googling, people suggested to remove @Id from subclass, but isn't every Entity requires an identifier?
That's wrong. Always question what you read on the Internet.
Yes, I found that example in your blog. That's where helped me understand my question earlier about the query. Thank you so much.
I've posted my Entity classes.
I was using Hibernate 5.0.2 but just upgraded to 5.2.10.Final and still getting this exception.
Code:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.ClassCastException: org.hibernate.mapping.Formula cannot be cast to org.hibernate.mapping.Column
Code:
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity(name = "BookShelf")
@Table(name = "bookshelf")
public class BookShelf {
@Id
@Setter
@Getter
@Column(name = "id", insertable = false, updatable = false)
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
@Getter
@Setter
@OneToMany(mappedBy = "bookShelf")
private List<Book> books = new ArrayList<>();
}
Code:
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.JoinFormula;
import javax.persistence.*;
@Entity(name = "Book")
@Table(name = "book")
public class Book {
@Id
@Setter
@Getter
@Column(name = "id", insertable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Getter
@Setter
@Column(name = "name")
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinFormula("select b.id from bookshelf b where id = any(b.book_ids)")
private BookShelf bookShelf ;
}