java 自动为@Embeddable 类的列名添加前缀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3046677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:06:06  来源:igfitidea点击:

Automatically Add a Prefix to Column Names for @Embeddable Classes

javahibernateorm

提问by VeeArr

I am developing a project in which I am persisting some POJOs by adding Hibernate annotations. One problem I am running into is that code like this fails, as Hibernate tries to map the sub-fields within the Time_Tonto the same column (i.e. startTime.secand stopTime.secboth try to map to the colum sec, causing an error).

我正在开发一个项目,在该项目中我通过添加 Hibernate 注释来保留一些 POJO。我遇到的一个问题是,像这样的代码失败了,因为 Hibernate 尝试将 内的子字段映射Time_T到同一列(即startTime.secstopTime.sec两者都尝试映射到 colum sec,导致错误)。

@Entity
public class ExampleClass
{
  @Id
  long eventId;

  Time_T startTime;
  Time_T stopTime;
}

@Embeddable
public class Time_T
{
  int sec;
  int nsec;
}

As there will be many occurrences like this throughout the system, it would be nice if there was an option to automatically append a prefix to the column name (e.g. make the columns be startTime_sec, startTime_nsec, stopTime_sec, stopTime_nsec), without having to apply overrides on a per-field basis. Does Hibernate have this capability, or is there any other reasonable work-around?

由于在整个系统中会出现很多这样的情况,如果有一个选项可以自动将前缀附加到列名(例如,使列成为startTime_sec, startTime_nsec, stopTime_sec, stopTime_nsec),而不必对每个应用程序进行覆盖,那就太好了。场基础。Hibernate 是否具有此功能,或者是否有其他合理的解决方法?

采纳答案by Bozho

Try setting the property hibernate.ejb.naming_strategyto org.hibernate.cfg.DefaultComponentSafeNamingStrategy

尝试将属性设置hibernate.ejb.naming_strategyorg.hibernate.cfg.DefaultComponentSafeNamingStrategy

回答by Farrukh Najmi

In my case with org.hibernate:hibernate-core:5.0.12.Final and org.springframework.boot:spring-boot-starter-data-jpa:1.5.2.RELEASE I had to do the following properties in my application.properties file:

在我使用 org.hibernate:hibernate-core:5.0.12.Final 和 org.springframework.boot:spring-boot-starter-data-jpa:1.5.2.RELEASE 的情况下,我必须在我的应用程序中执行以下属性。属性文件:

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

回答by Matheus Moreira

Another way to solve the problem is by using @AttributeOverrides and @AttributeOverride annotations. In your example the Time_T.secproperty is mapped to seccolumn. You could map ExampleClass like this:

另一种解决问题的方法是使用@AttributeOverrides 和@AttributeOverride 注释。在您的示例中,该Time_T.sec属性映射到sec列。你可以像这样映射 ExampleClass:

@Entity
public class ExampleClass {
    @Id
    long eventId;

    @AttributeOverrides(
        @AttributeOverride(name = "sec", column = @Column(name = "start_sec"))
    )
    Time_T startTime;
    Time_T stopTime;
}

The result mapping is startTime.sec <=> start_secand stopTime.sec <=> sec. Of course you could use annotations to create a more meaningful name for stopTipe.seccolumn.

结果映射是startTime.sec <=> start_secstopTime.sec <=> sec。当然,您可以使用注释为stopTipe.sec列创建更有意义的名称。

回答by Prabir Palai

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl