Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Hibernate 5.2.3中定义可嵌入类的索引?_Hibernate_Jpa_Indexing - Fatal编程技术网

如何在Hibernate 5.2.3中定义可嵌入类的索引?

如何在Hibernate 5.2.3中定义可嵌入类的索引?,hibernate,jpa,indexing,Hibernate,Jpa,Indexing,我有一个可嵌入类,定义为 @Embeddable public class Observation{ @Column(name = "created") private LocalDateTime created; @Column(name="code") private string code --- } 我的实体类是 @Entity @Table(name = "mete_observation",

我有一个可嵌入类,定义为

    @Embeddable
    public class Observation{

     @Column(name = "created")
      private LocalDateTime created;

     @Column(name="code")
      private string code
      ---

    }  
我的实体类是

@Entity
@Table(name = "mete_observation", uniqueConstraints = @UniqueConstraint(columnNames = "code"),
public class MeterObservation{

 @Embedded
    @AttributeOverrides({ @AttributeOverride(name = "created", column = @Column(name = "ob_created")) })
    private Observation raw = null; 
}
我想创建索引观察/创建字段和观察/代码


创建索引的最佳方法是什么?我应该在可嵌入类中定义它们吗?或者我可以从实体类中定义它吗?我该如何定义?请举个例子。

向嵌入对象添加索引可能有点棘手。要使其正常工作,必须输入准确的列名。
由于创建的
列名被
AttributeOverrides
覆盖,索引列不被称为
created
,而被称为
ob\u created

在没有其他配置的情况下,索引如下所示:

@Entity
@Table(
    name = "mete_observation",
    uniqueConstraints = @UniqueConstraint(columnNames = "code"),
    indexes = {
        @Index(columnList = "ob_created", name = "idx_created"), // not 'created' but 'ob_created' as it is overwritten
        @Index(columnList = "code", name = "idx_code"),
    }
)
public class MeterObservation{
    @Embedded
    @AttributeOverrides({ @AttributeOverride(name = "created", column = @Column(name = "ob_created")) })
    private Observation raw = null; 
}
(使用JPA2.2.3和Hibernate 5.4.15进行测试。)


如果命名的隐式策略设置为
ImplicitNamingStrategyComponentPathImpl
,则会增加复杂性。这也会更改列名

@Entity
@Table(
    name = "mete_observation",
    uniqueConstraints = @UniqueConstraint(columnNames = "code"),
    indexes = {
        @Index(columnList = "ob_created", name = "idx_created"), // the AttributeOverride 'ob_created' beats the naming strategy
        @Index(columnList = "raw_code", name = "idx_code"), // adding 'raw_' for the naming strategy
    }
)
public class MeterObservation{
    @Embedded
    @AttributeOverrides({ @AttributeOverride(name = "created", column = @Column(name = "ob_created")) })
    private Observation raw = null; 
}

向嵌入对象添加索引可能有点棘手。要使其正常工作,必须输入准确的列名。
由于创建的
列名被
AttributeOverrides
覆盖,索引列不被称为
created
,而被称为
ob\u created

在没有其他配置的情况下,索引如下所示:

@Entity
@Table(
    name = "mete_observation",
    uniqueConstraints = @UniqueConstraint(columnNames = "code"),
    indexes = {
        @Index(columnList = "ob_created", name = "idx_created"), // not 'created' but 'ob_created' as it is overwritten
        @Index(columnList = "code", name = "idx_code"),
    }
)
public class MeterObservation{
    @Embedded
    @AttributeOverrides({ @AttributeOverride(name = "created", column = @Column(name = "ob_created")) })
    private Observation raw = null; 
}
(使用JPA2.2.3和Hibernate 5.4.15进行测试。)


如果命名的隐式策略设置为
ImplicitNamingStrategyComponentPathImpl
,则会增加复杂性。这也会更改列名

@Entity
@Table(
    name = "mete_observation",
    uniqueConstraints = @UniqueConstraint(columnNames = "code"),
    indexes = {
        @Index(columnList = "ob_created", name = "idx_created"), // the AttributeOverride 'ob_created' beats the naming strategy
        @Index(columnList = "raw_code", name = "idx_code"), // adding 'raw_' for the naming strategy
    }
)
public class MeterObservation{
    @Embedded
    @AttributeOverrides({ @AttributeOverride(name = "created", column = @Column(name = "ob_created")) })
    private Observation raw = null; 
}

在我要定义的MeterObservation实体的
MeterObservation
@NeilStockton的
@Table
注释中定义它们有什么问题。如果我定义像Observation.code这样的索引,就会产生问题。(表示代码不是公共变量)我找不到引用。在这种情况下,会在JPA提供程序上引发错误,因为“code”肯定是该表中的一列(当您使用JPA提供程序创建架构时,它是否会创建一个名为“code”的列?)@NeilStockton是的,它会创建列。因此,在Hibernate上引发一个错误。在我要定义的MeterObservation实体的
@MeterObservation
@NeilStockton的
@表
注释中定义它们有何问题。如果我定义像Observation.code这样的索引,就会产生问题。(表示代码不是公共变量)我找不到引用。在这种情况下,会在JPA提供程序上引发错误,因为“code”肯定是该表中的一列(当您使用JPA提供程序创建架构时,它是否会创建一个名为“code”的列?)@NeilStockton是的,它创建列。所以在Hibernate上引发一个bug。我们可以在可嵌入类上创建索引吗。我们可以在可嵌入类上创建索引吗。