我刚开始使用Hibernate,我似乎很难让Hibernate正确地将MySQL数据库(例如:https://launchpad.net/test-db/)逆向工程到Java类中。
在employees数据库模式中有几个表:
问题是,当我反向工程表(下面的配置和收入文件)时,Hibernate没有创建上面的4个java类,而是为我提供了6个带有粗体的Java类:
据我所知,对于具有复合主键的表,Hibernate正在创建一个新的"classnameID"类,该类包含两个复合键的唯一组合。相反,仅仅拥有一个与特定部门关联的DeptEmp类和一组/列表的emp_no,这将是非常棒的。
我怎么才能让冬眠不这么做呢?我在.reveng文件中尝试了很多东西,但似乎没有什么效果。
逆向工程档案:
<hibernate-reverse-engineering>
<table-filter match-catalog="employees" match-name="departments" />
<table-filter match-catalog="employees" match-name="dept_emp" />
<table-filter match-catalog="employees" match-name="employees" />
<table-filter match-catalog="employees" match-name="salaries" />
<table schema="employees" name="departments">
<primary-key>
<key-column name="dept_no" />
</primary-key>
</table>
<table schema="employees" name="employees">
<primary-key>
<key-column name="emp_no" />
</primary-key>
</table>
<table schema="employees" name="salaries">
<primary-key>
<key-column name="emp_no" />
<key-column name="from_date" />
</primary-key>
</table>
<table schema="employees" name="dept_emp">
<primary-key>
<key-column name="emp_no" />
<key-column name="dept_no" />
</primary-key>
</table>
</hibernate-reverse-engineering>配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/employees</property>
<property name="hibernate.connection.username">****</property>
<property name="hibernate.connection.password">****</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.pool_size">20</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
</session-factory>
</hibernate-configuration>谢谢!
发布于 2020-02-25 18:36:05
这种行为是由在一个表中有多个ID列导致的,这就是为什么部门表没有IDClass,而薪水有。
那你怎么才能避开这一切?在reveng文件中,为每个表设置一个唯一的ID。如果您不正确地处理表中的所有主键,这可能会导致代码中的一些问题,所以要小心。
另外,您可以拥有这个额外的类,只需将它们作为int或字符串来处理,只需正确地初始化它们并在实体中设置它们。希望我能帮助你,因为我有这个问题,这对我来说是很残酷的。
https://stackoverflow.com/questions/26105835
复制相似问题