Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude a specific table from being created by hibernate?

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.

like image 524
prettyvoid Avatar asked Nov 12 '15 20:11

prettyvoid


1 Answers

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;
}
like image 147
Tobias Liefke Avatar answered Nov 17 '22 11:11

Tobias Liefke