当前位置:编程学习 > JAVA >>

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

我有两张表,

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

表间是一对多的关系(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;
}
}
sqlserver hibernate 反转工程 --------------------编程问答-------------------- 无所谓主键  索引新建好就行了 --------------------编程问答-------------------- LZ,如果用反向,建议LZ还是只生成对应自己的实体属性字段。
对应的表与表之间的关联关系,还是自己去配吧
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,