【问题标题】:Exclude a specific table from being created by hibernate?排除休眠创建的特定表?
【发布时间】:2016-02-14 07:29:38
【问题描述】:

我有一个映射到视图的@Entity,这是它的外观

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;
}

这很好用,我可以使用 dao 从视图中检索记录。但是我在日志中注意到,Hibernate 实际上正在尝试创建表,但由于它已经存在而失败。

2015-11-12 21:56:34.841 错误 4204 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport:HHH000389:不成功: 创建表 user_profile (user_id bigint not null, avg_airtime 整数,avg_fuel_points 整数,avg_miles 整数,电子邮件 varchar(255),first_name varchar(255),flights_count 整数, farthest_flight 整数,last_name varchar(255),longest_flight 整数,most_visited_city varchar(255),tier_end 整数,tier_start 整数,主键 (user_id)) 2015-11-12 21:56:34.841 错误 4204 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport:表 'user_profile' 已经存在

我可以配置休眠使其跳过创建此类实体吗?我以为@Immutable 注释告诉Hibernate 跳过创建,但似乎这个注释只是为了防止对表的粗鲁操作。

【问题讨论】:

  • AFAIK,hibernate 不支持这个。
  • 请在此处查看答案:stackoverflow.com/questions/438146/… 您可能只想每次都使用“更新”,而不是“创建”架构
  • 感谢 cmets 的各位。 @Zilvinas 我希望当我有 create-drop 时让 spring 忽略这些实体。
  • 在这篇博文中,SchemaFilter 用于medium.com/@horiaconstantin/…

标签: spring hibernate jpa spring-data


【解决方案1】:

@Subselect 注释是 Hibernate 中唯一阻止为 @Entity 创建对应表的注释:

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

    @Id 
    public Long userId;

    public Long flightId;

    @Column(name = "flight_seq") 
    public Long flightSequence;
}

【讨论】:

  • 感谢您的信息。我应该在@Subselect 查询WHERE 子句中指定我想在查询中使用的参数吗?还是道就够了?我不确定何时会使用 subselect 注释中的语句。
  • DAO 就足够了——尤其是如果您想为视图构建不同的查询。子选择是表的替换:SELECT ... FROM table 替换为 SELECT ... FROM (select * from user_earning)
  • 非常有用!遗憾的是,虽然没有与 @Subselect annontation 等效的 JPA。 :(
猜你喜欢
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 2011-10-01
  • 1970-01-01
相关资源
最近更新 更多