0

我的数据库中有 3 个表

  • 地址
  • 个人地址

这是脚本 sql

CREATE TABLE IF NOT EXISTS `address` (
  `address_id` int(11) NOT NULL AUTO_INCREMENT,
  `address` varchar(30) NOT NULL,
  PRIMARY KEY (`address_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

CREATE TABLE IF NOT EXISTS `person` (
  `person_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  PRIMARY KEY (`person_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

CREATE TABLE IF NOT EXISTS `person_address` (
  `person_id` int(11) NOT NULL,
  `address_id` int(11) NOT NULL,
  PRIMARY KEY (`person_id`,`address_id`),
  KEY `address_id` (`address_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `person_address`
  ADD CONSTRAINT `person_address_ibfk_2` FOREIGN KEY (`address_id`) REFERENCES `address` (`address_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `person_address_ibfk_1` FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`) ON DELETE CASCADE ON UPDATE CASCADE;

这是 hibernate.reveng.xml 文件

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
  <schema-selection match-catalog="manyToManyTest"/>
  <table-filter match-name="person_address"/>
  <table-filter match-name="address"/>
  <table-filter match-name="person"/>
</hibernate-reverse-engineering>

问题是在生成pojos和hbm文件时,没有生成personAddress pojo(hibernate.cfg.xml中没有条目<mapping resource="pojos/PersonAddress.hbm.xml"/>),为什么?

4

1 回答 1

0

不应生成 PersonAddress 类和 PersonAddress.hbm.xml。原因是默认情况下您的数据库映射到两个实体:Person 和 Address 以及它们之间的多对多关系。

检查 Person.hbm.xml(和 Address.hbm.xml 中的逆)是否包含以下内容:

<set ...>
      ...
   <many-to-many class="Address">
</set>

如果是,那么一切都很好。

于 2012-05-06T10:46:57.647 回答