论坛首页 Java企业应用论坛

[提问]schemaExport产生的ddl语句有点不可理解。。

浏览 3527 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2003-12-08  
我建立一个简单的many-t0-many的例子。Book,reader。
关系是many-to-many。应该产生三张表,book,reader,book_reader才对。
但是通过xml配置文件,导出的ddl却是这样:
drop table borrowBooks;
drop table reader;
drop table book;
drop table readers;
create table borrowBooks (
   readerid BIGINT not null,
   bookid BIGINT not null,
   primary key (readerid, bookid);
);;
create table reader (
   readerid BIGINT not null generated by default as identity,
   name VARCHAR(255); not null,
   primary key (readerid);
);;
create table book (
   bookid BIGINT not null generated by default as identity,
   bookName VARCHAR(255); not null unique,
   bookstoreid BIGINT not null,
   bookStoreId BIGINT,
   primary key (bookid);
);;
create table readers (
   bookId BIGINT not null,
   readerid BIGINT not null,
   primary key (bookId, readerid);
);;
alter table borrowBooks add constraint FKF2DE79B5AD8B31C4 foreign key (bookid); references book;
alter table borrowBooks add constraint FKF2DE79B5CC52411E foreign key (readerid); references reader;
create index IXF2DE79B5CC52411E on borrowBooks (readerid);;
alter table readers add constraint FK4065C010AD8B2DE4 foreign key (bookId); references book;
alter table readers add constraint FK4065C010CC52411E foreign key (readerid); references reader;
create index IX4065C010AD8B2DE4 on readers (bookId);;


为什么会有四张表呢?虽然这样也可以实现,但明显冗余嘛。
我的reader.hbm.xml,book.hbm.xml分别是:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class name="com.jaqsoft.demo.Reader" table="reader" proxy="com.jaqsoft.demo.Reader">
		<id name="readerId" type="java.lang.Long" column="readerid" unsaved-value="0">
			<generator class="identity"/>
		</id>	
		<property
			name="name"
			type="java.lang.String"
			column="name"
			not-null="true"/>			
		<set
			name="borrowBooks"
			lazy="true"
			cascade="none">
			<key column="readerid"/>
			<many-to-many 
				column="bookid"
				outer-join="auto"
				class="com.jaqsoft.demo.Book"
			/>
		</set>
	</class>

</hibernate-mapping>
<!-- parsed in 0ms -->

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class name="com.jaqsoft.demo.Book" table="book" proxy="com.jaqsoft.demo.Book">
		<id name="bookId" type="java.lang.Long" column="bookid" unsaved-value="0">
			<generator class="identity"/>
		</id>
		<property
			name="bookName"
			type="java.lang.String"
			column="bookName"
			not-null="true"
			unique="true"/>
		<set
			name="readers"
			lazy="true"
			cascade="none">
			<key column="bookId"/>
			<many-to-many 
				column="readerid"
				outer-join="auto"
				class="com.jaqsoft.demo.Reader"
			/>
		</set>
	</class>
</hibernate-mapping>
<!-- parsed in 0ms -->

谁能告诉我这是为什么?如果我强行改用3张表,也就是说我不采用schemaExport导出的数据表结构,hibernate还能正常工作吗?
   发表时间:2003-12-08  
<set 
         name="borrowBooks" 
         lazy="true" 
         cascade="none"> 
         <key column="readerid"/> 
         <many-to-many 
            column="bookid" 
            outer-join="auto" 
            class="com.jaqsoft.demo.Book" 
         /> 
      </set>

set没有指定table="readers",这样hibernate就会为你生成一个和这个set同名的表。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics