我在使用 hibernate 生成 mysql 模式时遇到问题。我已经搜索了我的问题的答案,但我没有找到它(许多类似的问题,但不完全是我的问题)。
我需要将状态图映射到mysql(或转换表)中,并级联删除状态。一个州可能有多个州作为子代(多对多关系)。我成功地映射了 State 类和“下一个状态”关系,但没有在 DELETE CASCADE 上生成正确的 SQL 选项。
Java类如下:
public class State {
private Integer version;
private Integer oid;
private String name;
private Map<String, State> nextStates;
public State(String name) {
super();
this.name = name;
this.nextStates = new HashMap<String, State>();
}
/**
* Adds some state to the transitions table.
* The table is indexed by states names.
*
*/
public void addNextState(State s) {
this.nextStates.put(s.getName(), s);
}
(...)
休眠配置是:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/testStatesDB</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">pass</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<mapping resource="State.hbm.xml" />
</session-factory>
</hibernate-configuration>
状态映射如下:
<hibernate-mapping>
<class name="State" table="state" optimistic-lock="version">
<id name="oid" column="OID">
<generator class="increment" />
</id>
<version column="version" name="version" unsaved-value="null"/>
<property name="name" type="java.lang.String"/>
<map name="nextStates" table="nextStates" cascade="delete" optimistic-lock="true">
<key column="parentState_oid" />
<index column="children_stateName" type="java.lang.String"/>
<many-to-many column="children_oid" class="State" />
</map>
</class>
</hibernate-mapping>
它创建了以下架构:
Hibernate:
create table nextStates (
parentState_oid integer not null,
children_oid integer not null,
children_stateName varchar(255) not null,
primary key (parentState_oid, children_stateName)
) ENGINE=InnoDB
Hibernate:
create table state (
OID integer not null,
version integer not null,
name varchar(255),
primary key (OID)
) ENGINE=InnoDB
Hibernate:
alter table nextStates
add index FKB4A102D5587BDC23 (parentState_oid),
add constraint FKB4A102D5587BDC23
foreign key (parentState_oid)
references state (OID)
Hibernate:
alter table nextStates
add index FKB4A102D5F71F7FB (children_oid),
add constraint FKB4A102D5F71F7FB
foreign key (children_oid)
references state (OID)
例如,考虑运行以下测试代码,
State pend = new State("Pending");
State dev = new State("In development");
State finali = new State("Finalized");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
pend.addNextState(dev);
dev.addNextState(finali);
dev.addNextState(pend);
session.save(pend);
session.save(dev);
session.save(finali);
session.getTransaction().commit();
session.beginTransaction();
dev = (State)session.load(State.class, 3);
session.delete(dev);
session.getTransaction().commit();
我收到以下错误消息:
Cannot delete or update a parent row: a foreign key constraint fails (`testStatesDB`.`nextstates`, CONSTRAINT `FKB4A102D5F71F7FB` FOREIGN KEY (`children_oid`) REFERENCES `state` (`OID`))
如果我转到数据库并将属性 ON DELETE CASCADE 添加到生成的外键中,该示例将按我的需要工作。
问题是:这是使用 HashMaps 映射多对多关系的正确方法吗?如果正确,如何使用休眠 xml 映射生成 ON DELETE CASCADE 选项?
我一直在处理这个问题很长时间,但我没有找到一个好的解决方案。