如何在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。我们可以在可嵌入类上创建索引吗。我们可以在可嵌入类上创建索引吗。