Hibernate 3.0 中的条件映射?

发布于 2024-09-08 19:22:37 字数 448 浏览 1 评论 0原文

如何在 hibernate 中创建依赖于 type 属性的映射,将数据插入适当的列或从适当的列检索数据。

结构:

表列:

|TYPE | CHARACTER | DATE | TIME | NUMERIC|

POJO:

class Pojo {

 private int type;
 private Object data;

 ...

}

示例:

  • 插入/更新
    如果类型为 1,我们将值输入到列 CHARACTER

  • Select
    如果类型为 2,我们从 NUMERIC 列获取值

如果类型为 2,我们从 NUMERIC TIP : 我们有两列并且对结果进行透视的结构不适合这种情况。

How to create mapping in hibernate that depends on the type property, insert/retrieve the data into/from proper column.

Structure:

TABLE COLUMNS:

|TYPE | CHARACTER | DATE | TIME | NUMERIC|

POJO:

class Pojo {

 private int type;
 private Object data;

 ...

}

Examples:

  • Insert/Update
    If the type is 1 we input the value to column CHARACTER

  • Select
    If the type is 2 we get the value from column NUMERIC

TIP:
The structure where we have two columns and we PIVOT the result is not a option for this case.

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

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

发布评论

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

评论(2

刘备忘录 2024-09-15 19:22:37

我为此选择的解决方案是使用整数鉴别器映射每个类层次结构的表。

我创建了一个基本的抽象泛型类,用于存储

 public abstract class ValueBase<T> {

   private Long id; 
   private int valueType;
   private T value;

   public T getValue(){
     return value;
   }

   public void setValue(T value){
     this.value = value;
   }

   protected void setValueType(int valueType) {
      this.valueType = valueType;
   }

   public int getValueType(){
     return this.valueType;
   }

   private void setID(Long id) {
      this.id = id;
   }

   public int getID(){
     return this.id;
   }


 }

我创建的类型管理对象和接口,但可以使用 Arthur 提出的封装进行枚举。

public interface IValueTypes() {

 public static final int NONE = -1;
 public static final int NUMERIC = 0;
 public static final int CHARACTER = 1;
 public static final int DATE = 2;
 public static final int TIME = 3;
 public static final int BOOLEAN = 4;

}

对于我创建的每种类型,类

 public class NumericType extedns ValueBase<BigDecinal> {

    public NumericType(){
      super();
      setValueType(IValueTypes.NUMERIC);
    }

 }

 public class CharacterType extedns ValueBase<String> {

    public NumericType(){
      super();
      setValueType(IValueTypes.CHARACTER);
    }

 }

提示:为了允许使用数字作为鉴别器值,我们需要首先为我们正在映射的类指定它,因为默认情况下使用类名 [String],并且我们尝试使用子类映射的数字鉴别器我们会得到一个休眠错误,说存在某种类型不匹配。

<class name="TestValue" table="TestValues" schema="TEST" abstract="true" discriminator-value="-1"> 
  <id name="id" type="java.lang.Long">
    <column name="VALUE_ID" precision="22" scale="0" />
    <generator class="native"/>
  </id>

  <discriminator column="TYPE_VALUE" not-null="false" type="integer"/> 

  <subclass discriminator-value="0" name="NumericType">
    <property name="value" type="java.math.BigDecimal" column="NUMERIC" not-null="false"/>
  </subclass>   

  <subclass discriminator-value="1" name="CharacterType">
    <property name="value" type="java.lang.String" column="CHARACTER" not-null="false"/>
  </subclass>   

</class>

虽然我们从数据库检索对象始终位于给定类型的类中,但我们可以轻松地对其进行操作,并且我们通过保存基类来使用一个位置来保存它们。

The solution that i have choose for this is table per class hierarchy mapping with an integer discriminator.

I have created a base abstract generic class that store the object

 public abstract class ValueBase<T> {

   private Long id; 
   private int valueType;
   private T value;

   public T getValue(){
     return value;
   }

   public void setValue(T value){
     this.value = value;
   }

   protected void setValueType(int valueType) {
      this.valueType = valueType;
   }

   public int getValueType(){
     return this.valueType;
   }

   private void setID(Long id) {
      this.id = id;
   }

   public int getID(){
     return this.id;
   }


 }

For Type management i have created and interface, but there is possible to enumeration using the encapsulation presented by Arthur.

public interface IValueTypes() {

 public static final int NONE = -1;
 public static final int NUMERIC = 0;
 public static final int CHARACTER = 1;
 public static final int DATE = 2;
 public static final int TIME = 3;
 public static final int BOOLEAN = 4;

}

The for each type I have created class

 public class NumericType extedns ValueBase<BigDecinal> {

    public NumericType(){
      super();
      setValueType(IValueTypes.NUMERIC);
    }

 }

 public class CharacterType extedns ValueBase<String> {

    public NumericType(){
      super();
      setValueType(IValueTypes.CHARACTER);
    }

 }

TIP: To allow use the a number as discriminator value we need to specify it first for the class that we are mapping, because by default is used class name [String] and i we try to use the numeric discriminator for subclass mapping we will get a hibernate error say that there is some type mismatch.

<class name="TestValue" table="TestValues" schema="TEST" abstract="true" discriminator-value="-1"> 
  <id name="id" type="java.lang.Long">
    <column name="VALUE_ID" precision="22" scale="0" />
    <generator class="native"/>
  </id>

  <discriminator column="TYPE_VALUE" not-null="false" type="integer"/> 

  <subclass discriminator-value="0" name="NumericType">
    <property name="value" type="java.math.BigDecimal" column="NUMERIC" not-null="false"/>
  </subclass>   

  <subclass discriminator-value="1" name="CharacterType">
    <property name="value" type="java.lang.String" column="CHARACTER" not-null="false"/>
  </subclass>   

</class>

While we retrieve the object from database is always in given typed class that we can operate easily on it, and we use one place to save them by saving the base class.

度的依靠╰つ 2024-09-15 19:22:37

这种行为是通过使用封装来实现的。例如,我在数据库(INSERT/UPDATE)中有一个字符串(Y/N)列,它由布尔值(SELECT)封装,如下所示。

public class SomeEntity {

    private boolean checked;

    private boolean isChecked() {
        return this.checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public String getCheckedAsString() {
        return checked ? "Y" : "N";
    }

    public void setCheckedAsString(String checked) {
        this.checked = checked.equals("Y") ? true : false;
    }

}

您的映射位于此处

<property name="checkedAsString" type="string" column="CHECKED"/>

此策略已解释此处 我使用了除了转换之外的解决方法,以允许 String Java 关键字用作枚举。请参阅此处

This kind of behavior is accomplished by using encapsulation. For instance, i have a String (Y/N) column in database (INSERT/UPDATE) which is encapsulated by a boolean (SELECT) as follows

public class SomeEntity {

    private boolean checked;

    private boolean isChecked() {
        return this.checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public String getCheckedAsString() {
        return checked ? "Y" : "N";
    }

    public void setCheckedAsString(String checked) {
        this.checked = checked.equals("Y") ? true : false;
    }

}

Your mapping goes here

<property name="checkedAsString" type="string" column="CHECKED"/>

This strategy is explained here I have used, besides conversion, as a workaround To allow String Java keywords To be used as Enum. See here

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