Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 休眠一对一关系映射_Sql_Hibernate_Database Design_Shopping Cart_One To One - Fatal编程技术网

Sql 休眠一对一关系映射

Sql 休眠一对一关系映射,sql,hibernate,database-design,shopping-cart,one-to-one,Sql,Hibernate,Database Design,Shopping Cart,One To One,我为一个购物卡应用程序在两个表之间建立了一对一的关系,即客户和卡之间的关系 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_

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

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



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

您如何使用hbm或schema ddl direclty生成实体?我使用hbm生成实体已进行编辑。您似乎将客户卡映射为一组?生成的代码反映了这种映射是合理的。但是在上面的脚本中,我将customer\u id
customer\u id
INT设置为唯一的,以避免将其转换为一对多关系
<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>