2

由于 mssql 限制为 900 字节,我需要减少索引大小。

我有一个类,它有一个声明为集合的集合。因此,主键由包括外键在内的所有非空列组成。从此主键创建一个索引。我不需要索引覆盖所有这些列。

有没有办法在不改变整个数据结构设置的情况下减小索引大小?

这是周围类定义中集合的当前配置:

  <set cascade="save-update,persist,merge,refresh,replicate,evict,delete,delete-orphan" fetch="select" lazy="true" table="mySubsetTable" batch-size="1000" name="attributes">
    <key foreign-key="FK_Mothertable">
      <column name="number"/>
      <column name="data"/>
    </key>
    <composite-element class="MySubsetElement">
      <property name="type" length="200" not-null="true" type="class"/>
      <property name="attribute" length="2000" column="attrValue" not-null="false"/>
      <property name="myboolean" type="boolean">
        <column name="myboolean"/>
      </property>
      <property name="anotherAttribute" length="200"/>
      <property name="evenAnotherAttribute" length="200" not-null="true"/>
      <property name="evenOneMoreAttribute" not-null="true">
        <type name="SomeClass">
          <param name="enumClass">someEnumClass</param>
        </type>
      </property>
    </composite-element>
  </set>

我目前正在使用带有 xdoclet 注释的休眠 3.3.1:

  /**
   * Attributes of this matchable
   * 
   * @hibernate.set table="mySubsetTable" cascade="save-update,persist,merge,refresh,replicate,evict,delete,delete-orphan" lazy="true"
   *                batch-size="1000" fetch="select"
   * @hibernate.key foreign-key="FK_Mothertable"
   * @hibernate.key-column name="number"
   * @hibernate.key-column name="data"
   * @hibernate.composite-element class="MySubsetElement"
   */
   public Set<MySubsetElement> getSubsetElements() { ... }

非常感谢您的建议!

(请不要让我参考http://docs.jboss.org/hibernate/ 我已经找到了这个。)

编辑 我不能减小所有属性的大小以适应大小限制。由外键组成的索引就足够了。此外,我真的很想要一个不会改变底层数据结构的解决方案,因为我正在开发一个已经在使用的产品。

4

2 回答 2

1

您正在为您的集合使用复合元素。这可能确实是“正确”的方式,因为所有MySubsetElements 都依赖于它们的所有者,但正如您现在所看到的,它也会对关系模型产生影响。

我建议采用以下方法(我正在使用注释,您可能希望将其转换为您的映射配置):

@Entity
class MySubsetElement {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;

  @ManyToOne(optional=false)
  private MyParentElement owner;

  public MySubsetElement( MyParentElement owner ) {
    ...
  }

}

@Entity
public class MyParentElement {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;

  @OneToMany(mappedBy="owner", cascade={CascadeType.ALL})
  private Set<MySubsetElement> children;

}
于 2016-05-06T11:52:46.917 回答
0

以下是我如何实现吉米的建议:

<hibernate-mapping>
    <class name="MyParent" ....>
      ...
      <set cascade="save-update,persist,merge,refresh,replicate,evict,delete,delete-orphan" fetch="select" lazy="true" table="SubsetTable" batch-size="1000" name="attributes">
        <key foreign-key="FK_ParentTable" not-null="true">
          <column name="number"/>
          <column name="data"/>
        </key>
        <one-to-many class="MySubset" entity-name="MySubsetentity"/>
      </set>
      ...
    </class>

    <class name="MySubset" ....>
          <id name="id" type="long">
            <column name="id"/>
            <generator class="MyIdGeneratorClass">
              <param name="sequence">mySequence</param>
            </generator>
          </id>
          <property name="type" length="200" not-null="true" type="class"/>
          <property name="attribute" length="2000" column="attrValue" not-null="false"/>
          <property name="myboolean" type="boolean">
            <column name="myboolean"/>
          </property>
          <property name="anotherAttribute" length="200"/>
          <property name="evenAnotherAttribute" length="200" not-null="true"/>
          <property name="evenOneMoreAttribute" not-null="true">
            <type name="SomeClass">
              <param name="enumClass">someEnumClass</param>
            </type>
          </property>   
    </class>
</hibernate-mapping>

重要的部分是父子集定义not-null="true"的标签内部。key这使子集能够保持对父级的无知。

于 2016-05-11T13:51:13.393 回答