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

Édo*_*ard 10 java configuration hibernate

我正在学习Java和Hibernate.现在,我无法理解如何使用自定义物理命名策略:虽然PhysicalNamingStrategy对象确实是实例化的,但是从不调用toPhysicalTableName或者toPhysicalColumnName调用方法 - 至少我没有看到调试器.

版本: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
Run Code Online (Sandbox Code Playgroud)
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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
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());
    }
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

这是我得到的表格:

[cake]> SELECT * FROM cake;
+----+-----------+-----------------------+---------+
| id | FLAVOUR   | name                  | sErViNg |
+----+-----------+-----------------------+---------+
|  0 | chocolate | Molten Chocolate Cake |       1 |
+----+-----------+-----------------------+---------+
Run Code Online (Sandbox Code Playgroud)

我希望:

+----+-----------+-----------------------+---------+
| ID | FLAVOUR   | NAME                  | SERVING |
+----+-----------+-----------------------+---------+
|  0 | chocolate | Molten Chocolate Cake |       1 |
+----+-----------+-----------------------+---------+
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

Ste*_*ers 6

这没有很好的记录,但不幸的是 Hibernate 似乎不支持在 hibernate.cfg.xml 中设置的特定属性。引用一篇非常古老的 Hibernate 论坛帖子

您只能在 hibernate.properties 或 hibernate.cfg.xml 中设置给定 Environment.java 类的属性。其余属性(如 NamingStrategy)必须使用 Configuration 类进行配置。

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


小智 3

也可以在配置中设置。

public class Main {

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

    try (
            Configuration configuration =new Configuration();
            configuration.setPhysicalNamingStrategy(new AllCapsPhysicalNamingStrategy());
            SessionFactory sessionFactory = 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();
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)