我的数据库中有以下表格。我希望表中的列booked_items
包含与表testuser
中用户关联的行数testbooking
。它当然可以在程序内部完成,但如果它可以在休眠映射和表 DDL 中完成,那就很脏了。
CREATE TABLE `testuser` (
`id` bigint(20) NOT NULL,
`username` varchar(30) DEFAULT NULL,
`password` varchar(128) DEFAULT NULL,
`booked_items` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `testbooking` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`item_id` bigint(20) NOT NULL,
`start_date` date DEFAULT NULL,
`end_date` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FKEC747367A12BE486` (`item_id`),
KEY `FKEC7473671463A66C` (`user_id`),
CONSTRAINT `FKEC7473671463A66C` FOREIGN KEY (`user_id`) REFERENCES `testuser`
(`id`),
CONSTRAINT `FKEC747367A12BE486` FOREIGN KEY (`item_id`) REFERENCES `testitem`
(`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
CREATE TABLE `testitem` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=latin1;
testuser 表的休眠映射为:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="my.hibernate.actors.User" discriminator-value="user"
table="testuser" catalog="efeu">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="increment" />
</id>
<discriminator column="user_type" type="string" length="8" />
<property name="username" type="java.lang.String" not-null="true"
unique="true" unique-key="true">
<column name="username" length="30" />
</property>
<property name="password" type="java.lang.String" not-null="true">
<column name="password" length="128" />
<subclass name="my.hibernate.actors.Subscriber"
discriminator-value="subsc">
<property name="bookedItems" type="java.lang.Integer" column="booked_items" />
<set name="bookings" inverse="true" cascade="all-delete-orphan">
<key column="user_id" />
<one-to-many class="my.hibernate.operations.Booking" />
</set>
</subclass>
<subclass name="my.hibernate.actors.Clerk"
discriminator-value="clerk" />
</class>
</hibernate-mapping>
testbooking 表的休眠映射为:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="my.hibernate.operations.Booking" table="testBooking" catalog="efeu">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="native" />
</id>
<many-to-one name="userID" column="user_id" cascade="persist"
not-null="true" class="my.hibernate.actors.Subscriber" />
<many-to-one name="itemID" column="item_id" cascade="persist"
not-null="true" class="my.hibernate.items.Item" />
<property name="startDate" type="java.sql.Date" not-null="true">
<column name="start_date" length="10" />
</property>
<property name="endDate" type="java.sql.Date">
<column name="end_date" length="10" />
</property>
</class>
</hibernate-mapping>
PS。我想针对这个问题提出 2 个单独的问题。一次用于 Hibernate,一次用于 SQL,但我担心它会被视为重复。对于 Hibernate 需要了解的 SQL,我很想知道。它是否取决于 SQL 的方言?我目前正在询问mysql方言。