使用 Hibernate 为用户实现 EAV 模式 ->设置关系

发布于 2024-08-29 11:49:53 字数 1902 浏览 12 评论 0原文

我正在尝试使用 Java/Spring MVC 和 Hibernate 在我的 Web 应用程序中设置一个简单的 EAV 模式。我似乎无法弄清楚这种情况下 hibernate XML 设置背后的魔力。

我的数据库表“SETUP”有三列:

  • user_id (FK)
  • setup_item
  • setup_value

数据库组合键由 user_id | user_id | user_id (FK) setup_item setup_value 组成。 setup_item

这是Setup.java类:

public class Setup implements CommonFormElements, Serializable {
  private Map data = new HashMap();
  private String saveAction;
  private Integer speciesNamingList;
  private User user;

  Logger log = LoggerFactory.getLogger(Setup.class);

  public String getSaveAction() {
    return saveAction;
  }

  public void setSaveAction(String action) {
    this.saveAction = action;
  }

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }

  public Integer getSpeciesNamingList() {
    return speciesNamingList;
  }

  public void setSpeciesNamingList(Integer speciesNamingList) {
    this.speciesNamingList = speciesNamingList;
  }

  public Map getData() {
    return data;
  }

  public void setData(Map data) {
    this.data = data;
  }
}

我的Hibernate设置问题是,我似乎无法弄清楚如何映射外键和映射的键将构造复合键的事实表的...这是由于缺乏使用 Hibernate 的经验。这是我最初尝试让它发挥作用:

<composite-id>
  <key-many-to-one foreign-key="id" name="user" column="user_id" class="Business.User">
    <meta attribute="use-in-equals">true</meta>
  </key-many-to-one>
</composite-id>

<map lazy="false" name="data" table="setup">
  <key column="user_id" property-ref="user"/>
  <composite-map-key class="Command.Setup">
    <key-property name="data" column="setup_item" type="string"/>
  </composite-map-key>

  <element column="setup_value" not-null="true" type="string"/>
</map>

任何有关如何正确映射这种常见场景的见解将不胜感激!

I'm trying to setup a simple EAV pattern in my web app using Java/Spring MVC and Hibernate. I can't seem to figure out the magic behind the hibernate XML setup for this scenario.

My database table "SETUP" has three columns:

  • user_id (FK)
  • setup_item
  • setup_value

The database composite key is made up of user_id | setup_item

Here's the Setup.java class:

public class Setup implements CommonFormElements, Serializable {
  private Map data = new HashMap();
  private String saveAction;
  private Integer speciesNamingList;
  private User user;

  Logger log = LoggerFactory.getLogger(Setup.class);

  public String getSaveAction() {
    return saveAction;
  }

  public void setSaveAction(String action) {
    this.saveAction = action;
  }

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }

  public Integer getSpeciesNamingList() {
    return speciesNamingList;
  }

  public void setSpeciesNamingList(Integer speciesNamingList) {
    this.speciesNamingList = speciesNamingList;
  }

  public Map getData() {
    return data;
  }

  public void setData(Map data) {
    this.data = data;
  }
}

My problem with the Hibernate setup, is that I can't seem to figure out how to map out the fact that a foreign key and the key of a map will construct the composite key of the table... this is due to a lack of experience using Hibernate. Here's my initial attempt at getting this to work:

<composite-id>
  <key-many-to-one foreign-key="id" name="user" column="user_id" class="Business.User">
    <meta attribute="use-in-equals">true</meta>
  </key-many-to-one>
</composite-id>

<map lazy="false" name="data" table="setup">
  <key column="user_id" property-ref="user"/>
  <composite-map-key class="Command.Setup">
    <key-property name="data" column="setup_item" type="string"/>
  </composite-map-key>

  <element column="setup_value" not-null="true" type="string"/>
</map>

Any insight into how to properly map this common scenario would be most appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2024-09-05 11:49:54

正如您自己所示,您有一个不一致映射

您说Setup类定义了一个复合主键(注意我已经创建了一个复合主键类(SetupId - 见下文),它必须实现Serialized、equals和hashcode方法)

package ar.domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Setup implements Serializable {

    private SetupId setupId;

    private User user;
    private Map data= new HashMap();

    public SetupId getSetupId() {
        return setupId;
    }

    public void setSetupId(SetupId setupId) {
        this.setupId = setupId;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Map getData() {
        return data;
    }

    public void setData(Map data) {
        this.data = data;
    }


    public static class SetupId implements Serializable {

        private Integer userId;
        private String setupItem;

        public String getSetupItem() {
            return setupItem;
        }

        public void setSetupItem(String setupItem) {
            this.setupItem = setupItem;
        }

        public Integer getUserId() {
            return userId;
        }

        public void setUserId(Integer userId) {
            this.userId = userId;
        }

        @Override
        public boolean equals(Object o) {
            if (o == null)
                return false;

            if (!(o instanceof SetupId))
                return false;

            final SetupId other = (SetupId) o;
            if (!(getUserId().equals(other.getUserId())))
                return false;
            if (!(getSetupItem().equals(other.getSetupItem())))
                return false;

            return true;
        }

        @Override
        public int hashCode() {
            int hash = 7;
            hash = 11 * hash + (getUserId() != null ? getUserId().hashCode() : 0);
            hash = 11 * hash + (getSetupItem() != null ? getSetupItem().hashCode() : 0);
            return hash;
        }

    }

}

因为您的Setup类有一个值类型的Map,所以您在定义其关系时应该定义其复合外键(参见key元素)

<class name="ar.domain.Setup">
    <composite-id name="setupId" class="ar.domain.Setup$SetupId">
        <key-property name="setupItem" type="string" column="SETUP_ITEM"/>
        <key-property name="userId" type="integer" column="USER_ID"/>
    </composite-id>
    <many-to-one name="user" class="ar.domain.User" column="USER_ID" insert="false" update="false"/>
    <map name="data" table="DATA_TABLE">
        <key>
            <column name="SETUP_ITEM"/>
            <column name="USER_ID"/>
        </key>
        <map-key column="USER_ID"/>
        <element column="SETUP_VALUE" not-null="true" type="string"/>
    </map>
</class>

,同时使用复合外键列作为映射键(USER_ID,对吗?),这没有意义。为什么 ?

  • Hibernate 不允许你更新(复合)主键列

除此之外,Hibernate 不支持自动生成复合主键

假设这里有你的 SETUP 表

SETUP_ITEM USER_ID
0          1
0          2

和你的 DATA_TABLE

SETUP_ITEM USER_ID
0          1

什么无论您尝试以下操作,都会发生这种情况。

Integer userId = 3;
String setupValue = "someValue";

setup.getData().put(userId, setupValue);

由于 SETUP 表没有定义值为 3 的 USER_ID,您将看到约束违规

牢记在心

当您有一个不可更新的(复合)主键时,避免使用它来更改依赖于它的可变属性。否则,Hibernate 会抱怨它。

As shown by yourself, you have a inconsistent mapping

You said Setup class defines a composite primary key (Notice i have created a composite primary key class (SetupId - see bellow) which must implements Serializable and equals and hashcode method)

package ar.domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Setup implements Serializable {

    private SetupId setupId;

    private User user;
    private Map data= new HashMap();

    public SetupId getSetupId() {
        return setupId;
    }

    public void setSetupId(SetupId setupId) {
        this.setupId = setupId;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Map getData() {
        return data;
    }

    public void setData(Map data) {
        this.data = data;
    }


    public static class SetupId implements Serializable {

        private Integer userId;
        private String setupItem;

        public String getSetupItem() {
            return setupItem;
        }

        public void setSetupItem(String setupItem) {
            this.setupItem = setupItem;
        }

        public Integer getUserId() {
            return userId;
        }

        public void setUserId(Integer userId) {
            this.userId = userId;
        }

        @Override
        public boolean equals(Object o) {
            if (o == null)
                return false;

            if (!(o instanceof SetupId))
                return false;

            final SetupId other = (SetupId) o;
            if (!(getUserId().equals(other.getUserId())))
                return false;
            if (!(getSetupItem().equals(other.getSetupItem())))
                return false;

            return true;
        }

        @Override
        public int hashCode() {
            int hash = 7;
            hash = 11 * hash + (getUserId() != null ? getUserId().hashCode() : 0);
            hash = 11 * hash + (getSetupItem() != null ? getSetupItem().hashCode() : 0);
            return hash;
        }

    }

}

Because of your Setup class has a Map of value Type, you should define its composite foreign key when defining its relationship (see key element)

<class name="ar.domain.Setup">
    <composite-id name="setupId" class="ar.domain.Setup$SetupId">
        <key-property name="setupItem" type="string" column="SETUP_ITEM"/>
        <key-property name="userId" type="integer" column="USER_ID"/>
    </composite-id>
    <many-to-one name="user" class="ar.domain.User" column="USER_ID" insert="false" update="false"/>
    <map name="data" table="DATA_TABLE">
        <key>
            <column name="SETUP_ITEM"/>
            <column name="USER_ID"/>
        </key>
        <map-key column="USER_ID"/>
        <element column="SETUP_VALUE" not-null="true" type="string"/>
    </map>
</class>

And, at the same time, use a composite foreign key column as map-key (USER_ID, right ?) which does not make sense. Why ?

  • Hibernate does not allow you update a (composite) primary key column

Besides that, Hibernate does not support automatic generation of composite primary key

Suppose here goes your SETUP Table

SETUP_ITEM USER_ID
0          1
0          2

And your DATA_TABLE

SETUP_ITEM USER_ID
0          1

What happens whether you try the following one

Integer userId = 3;
String setupValue = "someValue";

setup.getData().put(userId, setupValue);

Because of SETUP Table does not define a USER_ID which value is 3, you will see a constraint violation.

Keep it in mind

When you have a (composite) primary key, which can not be updatable, avoid to use it, someway, to change a mutable property which depends on it. Otherwise, Hibernate will complain it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文