自动为@Embeddable 类的列名添加前缀 [英] Automatically Add a Prefix to Column Names for @Embeddable Classes

查看:21
本文介绍了自动为@Embeddable 类的列名添加前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

@Entity公共类 ExampleClass{@ID长事件ID;Time_T 开始时间;Time_T 停止时间;}@Embeddable公开课 Time_T{整数秒;内秒;}

由于在整个系统中会出现很多这样的情况,如果有一个选项可以自动将前缀附加到列名(例如,使列成为 startTime_secstartTime_nsecstopTime_secstopTime_nsec),而不必在每个字段的基础上应用覆盖.Hibernate 是否具有此功能,或者是否有其他合理的解决方法?

解决方案

尝试将属性hibernate.ejb.naming_strategy设置为org.hibernate.cfg.DefaultComponentSafeNamingStrategy>

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_T onto the same column (i.e. startTime.sec and stopTime.sec both try to map to the colum sec, causing an error).

@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?

解决方案

Try setting the property hibernate.ejb.naming_strategy to org.hibernate.cfg.DefaultComponentSafeNamingStrategy

这篇关于自动为@Embeddable 类的列名添加前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆