Java 休眠列名问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/376093/
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
hibernate column name issues
提问by lucas
@Column(name="DateOfBirth")
private Date dateOfBirth;
I specifically need the above code to create a column named "DateOfBirth," instead Hibernate gives me a column named date_of_birth. How can I change this? Is there a web.xml property? I came across DefaultNamingStrategy and ImprovedNamingStrategy, but not sure how to specify one or the other.
我特别需要上面的代码来创建一个名为“DateOfBirth”的列,而不是 Hibernate 给我一个名为 date_of_birth 的列。我怎样才能改变这个?有 web.xml 属性吗?我遇到了 DefaultNamingStrategy 和改进的NamingStrategy,但不确定如何指定其中一个。
采纳答案by OscarRyz
Here is a possible workaround: if you name it dateofbirth
the column in the DB would be named like that, but the attribute name should be the same.
这是一个可能的解决方法:如果您dateofbirth
将其命名,则数据库中的列将被命名为这样,但属性名称应该相同。
Hibernate takes the camel case format to create/read database columns.
Hibernate 采用驼峰格式来创建/读取数据库列。
I've had this problem before. I worked with a legacy columns where there was no space in the column names "employeename", "employeerole", "departmentlocation". I hate it because all my beans properties had to be without camel case.
我以前遇到过这个问题。我使用了旧列,其中列名称“employeename”、“employeerole”、“departmentlocation”中没有空格。我讨厌它,因为我所有的 bean 属性都必须没有驼峰式外壳。
Database columns separated by "_" will be used to properly camelCase as you have just seen.
正如您刚刚看到的那样,由“_”分隔的数据库列将用于正确的驼峰式大小写。
回答by davetron5000
I'm not 100% sure, but don't you need to annotate the get method and not the private variable?
我不是 100% 确定,但是您不需要注释 get 方法而不是私有变量吗?
回答by Ryan Anderson
Put the @Column annotation on the getter:
将 @Column 注释放在 getter 上:
@Column(name="DateOfBirth")
public Date getDateOfBirth() {
...
}
回答by cliff.meyers
You can annotate either fields or getter methods, it doesn't make a difference. Can you post your full hibernate.cfg.xml or persistence.xml file?
您可以注释字段或 getter 方法,这没有区别。您可以发布完整的 hibernate.cfg.xml 或 persistence.xml 文件吗?
回答by lucas
The workaround proposed was to use @Column(name="dateofbirth"), which worked for my purposes.
建议的解决方法是使用@Column(name="dateofbirth"),它对我有用。
回答by Dan Vinton
FYI: The reason for the insertion of underscores is probably because you're using an ImprovedNamingStrategy. It's set on your Configurationobject. See herefor an example...
仅供参考:插入下划线的原因可能是因为您使用的是改进的命名策略。它设置在您的Configuration对象上。请参阅此处的示例...
If you don't want the underscores you can just not set the naming strategy, or set it to the DefaultNamingStrategy you discovered earlier.
如果您不想要下划线,您可以不设置命名策略,或者将其设置为您之前发现的 DefaultNamingStrategy。
回答by syedmuneer
ImprovedNamingStrategy has method addUnderscores() which is called from tableName() and columnName() you can implement your own naming strategy class and override these as per your choice
改进的命名策略具有从 tableName() 和 columnName() 调用的方法 addUnderscores() 您可以实现自己的命名策略类并根据您的选择覆盖这些
public class MyOwnNamingStrategy extends ImprovedNamingStrategy {
@Override
public String tableName(String tableName) {
//return addUnderscores(columnName); // skip this
return columnName; // if you want column name variable name same
//return changeAsYouWant(columnName); // as name sames
}
}
回答by Rahul Singh
Try putting this in
试试把这个放进去
application.properties
应用程序属性
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
回答by JavaDev
add below property in the case of spring boot.
在弹簧靴的情况下添加以下属性。
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl