没有自主主键的表间关联关系如何处理

山狮 2013-11-20 10:52:49
我有两张表,

一张:用户基本信息表
一张:用户详细信息表

表间是一对多的关系(one-to-many关系),即一个用户基本信息表可以对应到多个详细信息,那么在这种情况下,用hibernate反转工程如何进行处理。生成的表结构总是不对。


--------------------------------------------------用户基本信息表
create table customerInfo(
customerId int identity primary key,
customerName varchar(20) not null,
password varchar(20) not null,
email varchar(30) not null,
cash money default(0)
)
go
alter table customerInfo
add constraint CK_cash
check(cash>=0)
go

create table customerDetailInfo(
customerId int not null,
customerName varchar(20) not null,
tel varchar(13) not null,
email varchar(30),
address varchar(80) not null,
countMoney float,
qq varchar(12)
)
alter table customerDetailInfo
add constraint FK_customerId
foreign key(customerId) references customerInfo(customerId)
go



反转工程会自动生成三个POJO类,但是对应的只有两个.hbm.xml文件;

CustomerInfo.java POJO类如下:

public class CustomerInfo implements java.io.Serializable {

private Integer customerId;
private String customerName;
private String password;
private String email;
private Double cash;
private Set orderMains = new HashSet(0);
private Set customerDetailInfos = new HashSet(0);


public CustomerInfo() {

}

public CustomerInfo(String customerName, String password, String email) {
this.customerName = customerName;
this.password = password;
this.email = email;
}

public CustomerInfo(String customerName, String password, String email,
Double cash, Set orderMains, Set customerDetailInfos) {
this.customerName = customerName;
this.password = password;
this.email = email;
this.cash = cash;
this.orderMains = orderMains;
this.customerDetailInfos = customerDetailInfos;
}

/**
省略get set方法
*/

}


对应的CustomerInfo.hbm.xml文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.snail.dao.pojo.CustomerInfo" table="customerInfo" schema="dbo" catalog="bookStore">
<id name="customerId" type="java.lang.Integer">
<column name="customerId" />
<generator class="native"></generator>
</id>
<property name="customerName" type="java.lang.String">
<column name="customerName" length="20" not-null="true" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="20" not-null="true" />
</property>
<property name="email" type="java.lang.String">
<column name="email" length="30" not-null="true" />
</property>
<property name="cash" type="java.lang.Double">
<column name="cash" scale="4" />
</property>
<set name="orderMains" inverse="true">
<key>
<column name="customerId" not-null="true" />
</key>
<one-to-many class="com.snail.dao.pojo.OrderMain" />
</set>
<set name="customerDetailInfos" inverse="true">
<key>
<column name="customerId" not-null="true" />
</key>
<one-to-many class="com.snail.dao.pojo.CustomerDetailInfo" />
</set>
</class>
</hibernate-mapping>



生成的CustomerDetailInfo.java POJO类如下:

public class CustomerDetailInfo implements java.io.Serializable {

private CustomerDetailInfoId id;
private CustomerInfo customerInfo;


public CustomerDetailInfo() {
}

public CustomerDetailInfo(CustomerDetailInfoId id, CustomerInfo customerInfo) {
this.id = id;
this.customerInfo = customerInfo;
}


public CustomerDetailInfoId getId() {
return this.id;
}
//省略set get 方法

}


对应的配置文件如下


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.snail.dao.pojo.CustomerDetailInfo" table="customerDetailInfo" schema="dbo" catalog="bookStore">
<composite-id name="id" class="com.snail.dao.pojo.CustomerDetailInfoId">
<key-property name="customerId" type="java.lang.Integer">
<column name="customerId" />
</key-property>
<key-property name="customerName" type="java.lang.String">
<column name="customerName" length="20" />
</key-property>
<key-property name="tel" type="java.lang.String">
<column name="tel" length="13" />
</key-property>
<key-property name="email" type="java.lang.String">
<column name="email" length="30" />
</key-property>
<key-property name="address" type="java.lang.String">
<column name="address" length="80" />
</key-property>
<key-property name="countMoney" type="java.lang.Double">
<column name="countMoney" precision="53" scale="0" />
</key-property>
<key-property name="qq" type="java.lang.String">
<column name="qq" length="12" />
</key-property>
</composite-id>
<many-to-one name="customerInfo" class="com.snail.dao.pojo.CustomerInfo" update="false" insert="false" fetch="select">
<column name="customerId" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>





生成的CustomerDetailInfoId如下:

public class CustomerDetailInfoId implements java.io.Serializable {

private Integer customerId;
private String customerName;
private String tel;
private String email;
private String address;
private Double countMoney;
private String qq;

public CustomerDetailInfoId() {
}

public CustomerDetailInfoId(Integer customerId, String customerName,
String tel, String address) {
this.customerId = customerId;
this.customerName = customerName;
this.tel = tel;
this.address = address;
}

public CustomerDetailInfoId(Integer customerId, String customerName,
String tel, String email, String address, Double countMoney,
String qq) {
this.customerId = customerId;
this.customerName = customerName;
this.tel = tel;
this.email = email;
this.address = address;
this.countMoney = countMoney;
this.qq = qq;
}
}
...全文
1062 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingfuzhuzi1314 2013-12-06
  • 打赏
  • 举报
回复
LZ,如果用反向,建议LZ还是只生成对应自己的实体属性字段。 对应的表与表之间的关联关系,还是自己去配吧
careers1111 2013-11-20
  • 打赏
  • 举报
回复
无所谓主键 索引新建好就行了

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧