24

I have an @Entity which is mapped to a view, here is how it looks

import org.hibernate.annotations.Immutable;
import javax.persistence.*;

@Table(name = "user_earning")
@Entity
@Immutable
public class UserFlightEarning {
    @Id public Long userId;
    public Long flightId;
    @Column(name = "flight_seq") public Long flightSequence;
}

This works fine, I can retrieve records from the view using the dao. However I noticed in the logs that Hibernate is actually trying to create the table but failing because it already exists.

2015-11-12 21:56:34.841 ERROR 4204 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: create table user_profile (user_id bigint not null, avg_airtime integer, avg_fuel_points integer, avg_miles integer, email varchar(255), first_name varchar(255), flights_count integer, furthest_flight integer, last_name varchar(255), longest_flight integer, most_visited_city varchar(255), tier_end integer, tier_start integer, primary key (user_id)) 2015-11-12 21:56:34.841 ERROR 4204 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : Table 'user_profile' already exists

Can I configure hibernate so it skips the creation of such entities? I thought the @Immutable annotation tells Hibernate to skip the creation but it seems this annotation is only to prevent crud operations on the table.

prettyvoid
  • 3,446
  • 6
  • 36
  • 60
  • AFAIK, hibernate doesn't support this. – Bogdan Kobylynskyi Nov 12 '15 at 20:21
  • Please see the answer here: http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do you probably only want to use "update", not "create" schema every time – Zilvinas Nov 12 '15 at 20:21
  • Thanks for the comments guys. @Zilvinas I was hoping to make spring ignore those entities when i have `create-drop`. – prettyvoid Nov 12 '15 at 20:26
  • In this Blog Post, SchemaFilter is used for that https://medium.com/@horiaconstantin/excluding-hibernate-entities-from-auto-generation-bce86f8e6d94 – Chris Aug 14 '19 at 09:09

1 Answers1

34

The @Subselect annotation is the only annotation in Hibernate that prevents the creation of the corresponding table for an @Entity:

@Entity
@Subselect("select * from user_earning")
public class UserFlightEarning {

    @Id 
    public Long userId;

    public Long flightId;

    @Column(name = "flight_seq") 
    public Long flightSequence;
}
Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
  • Thanks for the information. Should I specify the parameters I want to use in my queries within the `@Subselect` query `WHERE` clause? or will the dao be enough? I'm not exactly sure when the statement inside the subselect annotation will be used. – prettyvoid Nov 14 '15 at 18:22
  • 1
    The DAO is enough - especially if you want to build different queries for the view. The subselect is the replacement for the table: `SELECT ... FROM table` is replaced with `SELECT ... FROM (select * from user_earning)` – Tobias Liefke Nov 14 '15 at 20:54
  • 1
    Very useful! Pity, that there isn't a JPA equivalent of `@Subselect` annontation though. :( – Jagger Feb 26 '17 at 10:58