简体   繁体   English

在SQL和Hibernate映射中根据另一个表中被引用的行的count()设置列的值到我的表

[英]Setting the value of a column according to the count() of the referred rows in another table to my table in SQL and hibernate mapping

I have the following tables in my database. 我的数据库中有以下表格。 I want the column booked_items in the testuser table to contain the number of rows associated with the user in the testbooking table. 我想列booked_itemstestuser表包含与该用户相关联的行数testbooking表。 It is surely can be done inside the programme but it is dirty if it can be done in the hibernate mapping and the table DDL. 当然可以在程序内部完成,但是如果可以在休眠映射和表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;

The hibernate mapping for the testuser table is: 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>

The hibernate mapping for the testbooking table is: 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. PS。 I wanted to ask 2 separate questions for this issue. 我想问两个独立的问题。 Once for Hibernate and once for SQL but I was afraid it would be considered as a duplicate. 一次用于Hibernate,一次用于SQL,但我担心它将被视为重复项。 For Hibernate need to know for SQL I'm interested to know. 对于Hibernate需要了解的SQL我很感兴趣。 And does it depend on the dialect of the SQL? 并且它取决于SQL的方言吗? I'm currently asking for mysql dialect. 我目前正在询问mysql方言。

You can use a property on the user class with a sql formula associated to it. 您可以在用户类上使用与SQL公式关联的属性。

<property name="booked_items" type="long" formula="(SELECT COUNT(tb.id) FROM testbooking AS tb WHERE tb.user_id = id)"/>

Have a look at this answer: seems like duplicate 看看这个答案: 好像重复

Calculated property with JPA / Hibernate 使用JPA / Hibernate计算属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM