0

我在购物卡应用程序的两个表之间建立了一对一的关系,即客户和卡之间的关系,

CREATE TABLE `card` (
'card_id' INT NOT NULL,
'product_id` INT NOT NULL,
`customer_id` INT UNIQUE ,
`product_quantity` INT,
PRIMARY KEY (`product_id`,`card_id`),
KEY `FK_Customer` (`customer_id`),
CONSTRAINT `FK_Customer` FOREIGN KEY (`customer_id`) REFERENCES `customer`(`customer_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `customer` (
`customer_id` INT NOT NULL AUTO_INCREMENT,
`email` varchar(100) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`photo` varchar(100) DEFAULT NULL,
`balance` double DEFAULT NULL,
PRIMARY KEY (`customer_id`),
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

问题是hibernate生成实体时,customer.java如下:

public class Customer implements java.io.Serializable {

private Integer customerId;
private String email;
private String name;
private String password;
private String photo;
private Double balance;
private Set cards = new HashSet(0);
}

.hbm

<hibernate-mapping>
<class name="Customer" table="customer" catalog="shopping_card">
    <id name="customerId" type="java.lang.Integer">
        <column name="customer_id" />
        <generator class="identity" />
    </id>
    <property name="email" type="string">
        <column name="email" length="100" />
    </property>
    <property name="name" type="string">
        <column name="name" length="100" />
    </property>
    <property name="password" type="string">
        <column name="password" length="100" />
    </property>
    <property name="photo" type="string">
        <column name="photo" length="100" />
    </property>
    <property name="balance" type="java.lang.Double">
        <column name="balance" precision="22" scale="0" />
    </property>
    <set name="cards" table="card" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="customer_id" not-null="true" unique="true" />
        </key>
        <one-to-many class="Card" />
    </set>
</class>
</hibernate-mapping>

一对一关系转换为一对多,我希望 Customer.java 有一个 Card 实例变量而不是一组卡片,有什么问题?

4

0 回答 0