问题
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.
回答1:
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;
}
来源:https://stackoverflow.com/questions/33680504/exclude-a-specific-table-from-being-created-by-hibernate