gpt4 book ai didi

java - 如何在 hibernate.cfg.xml 中配置物理命名策略?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:59:00 24 4
gpt4 key购买 nike

我正在学习 Java 和 Hibernate。现在,我无法理解如何使用自定义物理命名策略:虽然 PhysicalNamingStrategy 对象确实已实例化,但 toPhysicalTableNametoPhysicalColumnName 方法永远不会被调用——至少我用调试器看不到。

版本:Java 1.8,Hibernate 5.2.10.Final,macOS 10.12。

这是一个最小的项目:

@Entity
public class Cake {
@Id
private long id;
private String name;
private String FLAVOUR;
private int sErViNg;

public Cake(String name, String flavour, int serving) {
this.name = name;
this.FLAVOUR = flavour;
this.sErViNg = serving;
}

// getters and setters

public class Main {

public static void main (String[] args) {
Transaction tx = null;

try (
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
) {
tx = session.beginTransaction();

Cake cake = new Cake("Molten Chocolate Cake", "chocolate", 1);
session.save(cake);

tx.commit();
}
catch (Exception e) {
e.printStackTrace();
if ( tx != null ) {
tx.rollback();
}
}
}
}

public class AllCapsPhysicalNamingStrategy
extends PhysicalNamingStrategyStandardImpl implements Serializable {

public static final AllCapsPhysicalNamingStrategy INSTANCE
= new AllCapsPhysicalNamingStrategy();

@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return new Identifier(name.getText().toUpperCase(), name.isQuoted());
}

@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
return new Identifier(name.getText().toUpperCase(), name.isQuoted());
}
}

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/cake</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.physical_naming_strategy">com.example.AllCapsPhysicalNamingStrategy</property>
<mapping class="com.example.Cake"/>
</session-factory>
</hibernate-configuration>

这是我得到的表格:

[cake]> SELECT * FROM cake;
+----+-----------+-----------------------+---------+
| id | FLAVOUR | name | sErViNg |
+----+-----------+-----------------------+---------+
| 0 | chocolate | Molten Chocolate Cake | 1 |
+----+-----------+-----------------------+---------+

我希望:

+----+-----------+-----------------------+---------+
| ID | FLAVOUR | NAME | SERVING |
+----+-----------+-----------------------+---------+
| 0 | chocolate | Molten Chocolate Cake | 1 |
+----+-----------+-----------------------+---------+

我在这里做错了什么?

最佳答案

这没有很好的记录,但不幸的是,Hibernate 似乎不支持在 hibernate.cfg.xml 中设置的特定属性。引用自a very old Hibernate forum post :

You can set the properties given Environment.java class only in hibernate.properties or hibernate.cfg.xml. Rest of the properties like NamingStrategy has to be configured with Configuration class.

因此建议删除该属性,而是按照 Shiv Raghuwanshi 的建议在 Configuration 实例的代码中设置它。

关于java - 如何在 hibernate.cfg.xml 中配置物理命名策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46132696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com