问题描述
我有两张表,一张:用户基本信息表一张:用户详细信息表表间是一对多的关系(one-to-many关系),即一个用户基本信息表可以对应到多个详细信息,那么在这种情况下,用hibernate反转工程如何进行处理。生成的表结构总是不对。--------------------------------------------------用户基本信息表createtablecustomerInfo(customerIdintidentityprimarykey,customerNamevarchar(20)notnull,passwordvarchar(20)notnull,emailvarchar(30)notnull,cashmoneydefault(0))goaltertablecustomerInfoaddconstraintCK_cashcheck(cash>=0)gocreatetablecustomerDetailInfo(customerIdintnotnull,customerNamevarchar(20)notnull,telvarchar(13)notnull,emailvarchar(30),addressvarchar(80)notnull,countMoneyfloat,qqvarchar(12))altertablecustomerDetailInfoaddconstraintFK_customerIdforeignkey(customerId)referencescustomerInfo(customerId)go
反转工程会自动生成三个POJO类,但是对应的只有两个.hbm.xml文件;CustomerInfo.javaPOJO类如下:publicclassCustomerInfoimplementsjava.io.Serializable{privateIntegercustomerId;privateStringcustomerName;privateStringpassword;privateStringemail;privateDoublecash;privateSetorderMains=newHashSet(0);privateSetcustomerDetailInfos=newHashSet(0);publicCustomerInfo(){}publicCustomerInfo(StringcustomerName,Stringpassword,Stringemail){this.customerName=customerName;this.password=password;this.email=email;}publicCustomerInfo(StringcustomerName,Stringpassword,Stringemail,Doublecash,SetorderMains,SetcustomerDetailInfos){this.customerName=customerName;this.password=password;this.email=email;this.cash=cash;this.orderMains=orderMains;this.customerDetailInfos=customerDetailInfos;}/**省略getset方法*/}
对应的CustomerInfo.hbm.xml文件<?xmlversion="1.0"encoding="utf-8"?><!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--MappingfileautogeneratedbyMyEclipsePersistenceTools--><hibernate-mapping><classname="com.snail.dao.pojo.CustomerInfo"table="customerInfo"schema="dbo"catalog="bookStore"><idname="customerId"type="java.lang.Integer"><columnname="customerId"/><generatorclass="native"></generator></id><propertyname="customerName"type="java.lang.String"><columnname="customerName"length="20"not-null="true"/></property><propertyname="password"type="java.lang.String"><columnname="password"length="20"not-null="true"/></property><propertyname="email"type="java.lang.String"><columnname="email"length="30"not-null="true"/></property><propertyname="cash"type="java.lang.Double"><columnname="cash"scale="4"/></property><setname="orderMains"inverse="true"><key><columnname="customerId"not-null="true"/></key><one-to-manyclass="com.snail.dao.pojo.OrderMain"/></set><setname="customerDetailInfos"inverse="true"><key><columnname="customerId"not-null="true"/></key><one-to-manyclass="com.snail.dao.pojo.CustomerDetailInfo"/></set></class></hibernate-mapping>
生成的CustomerDetailInfo.javaPOJO类如下:publicclassCustomerDetailInfoimplementsjava.io.Serializable{privateCustomerDetailInfoIdid;privateCustomerInfocustomerInfo;publicCustomerDetailInfo(){}publicCustomerDetailInfo(CustomerDetailInfoIdid,CustomerInfocustomerInfo){this.id=id;this.customerInfo=customerInfo;}publicCustomerDetailInfoIdgetId(){returnthis.id;}//省略setget方法}
对应的配置文件如下<?xmlversion="1.0"encoding="utf-8"?><!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--MappingfileautogeneratedbyMyEclipsePersistenceTools--><hibernate-mapping><classname="com.snail.dao.pojo.CustomerDetailInfo"table="customerDetailInfo"schema="dbo"catalog="bookStore"><composite-idname="id"class="com.snail.dao.pojo.CustomerDetailInfoId"><key-propertyname="customerId"type="java.lang.Integer"><columnname="customerId"/></key-property><key-propertyname="customerName"type="java.lang.String"><columnname="customerName"length="20"/></key-property><key-propertyname="tel"type="java.lang.String"><columnname="tel"length="13"/></key-property><key-propertyname="email"type="java.lang.String"><columnname="email"length="30"/></key-property><key-propertyname="address"type="java.lang.String"><columnname="address"length="80"/></key-property><key-propertyname="countMoney"type="java.lang.Double"><columnname="countMoney"precision="53"scale="0"/></key-property><key-propertyname="qq"type="java.lang.String"><columnname="qq"length="12"/></key-property></composite-id><many-to-onename="customerInfo"class="com.snail.dao.pojo.CustomerInfo"update="false"insert="false"fetch="select"><columnname="customerId"not-null="true"/></many-to-one></class></hibernate-mapping>
生成的CustomerDetailInfoId如下:publicclassCustomerDetailInfoIdimplementsjava.io.Serializable{privateIntegercustomerId;privateStringcustomerName;privateStringtel;privateStringemail;privateStringaddress;privateDoublecountMoney;privateStringqq;publicCustomerDetailInfoId(){}publicCustomerDetailInfoId(IntegercustomerId,StringcustomerName,Stringtel,Stringaddress){this.customerId=customerId;this.customerName=customerName;this.tel=tel;this.address=address;}publicCustomerDetailInfoId(IntegercustomerId,StringcustomerName,Stringtel,Stringemail,Stringaddress,DoublecountMoney,Stringqq){this.customerId=customerId;this.customerName=customerName;this.tel=tel;this.email=email;this.address=address;this.countMoney=countMoney;this.qq=qq;}}
解决方案
解决方案二:
无所谓主键索引新建好就行了
解决方案三:
LZ,如果用反向,建议LZ还是只生成对应自己的实体属性字段。对应的表与表之间的关联关系,还是自己去配吧