【发布时间】:2021-03-15 09:54:41
【问题描述】:
我有两个表,如下所述:
+-------------------+ +------------------------+
| TB_CONFIG | | TB_CONFIG_LANG |
+-------------------+ +------------------------+
| Customer_Id (PK) | | Product_Id (PK)|
| Company_Id (PK) | | Config_Id (PK)|
| Product_Id (PK) | | Language_Id (PK)|
| Config_Id (PK) | | Config_Description |
| Config_Value | | Config_Hint |
+-------------------+ | Config_Help |
+------------------------+
TB_CONFIG 数据是关于由指定的“客户”和“公司”在我们拥有的“产品(解决方案)”上设置的“配置”。
TB_CONFIG_LANG 存储有关各种语言的所有配置的描述、提示和帮助的数据,例如“EN_US”、“PT_BR”等。
我们总是将语言作为参数传递给将用于加载数据的JPQL,但我无法实现映射此连接的“正确方式”...
实际上,这些表映射如下:
TB_CONFIG_LANG 主键
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Embeddable
@MappedSuperclass
public class PkConfigurationLang implements Serializable {
@Column(name = "PRODUCT_ID")
@EqualsAndHashCode.Include
private int productId;
@Column(name = "CONFIG_ID")
@EqualsAndHashCode.Include
private String configId;
@Column(name = "LANGUAGE_ID")
@EqualsAndHashCode.Include
private String languageId;
}
TB_CONFIG_LANG 表
@Getter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Entity
@Table(schema = "CLOUD_CATALOG", name = "TB_CONFIG_LANG")
@Cache(region = "oConfigurationsCache", usage = CacheConcurrencyStrategy.READ_WRITE)
public class ConfigurationLanguage {
@EmbeddedId
private PkConfigurationLang id;
@Column(name = "CONFIG_DESCRIPTION")
private String name;
@Column(name = "CONFIG_HINT")
private String description;
@Column(name = "CONFIG_HELP")
private String help;
}
TB_CONFIG 主键
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Embeddable
@MappedSuperclass
public class PkConfiguration implements Serializable {
@Column(name = "CUSTOMER_ID")
@EqualsAndHashCode.Include
private int customerId;
@Column(name = "COMPANY_ID")
@EqualsAndHashCode.Include
private int companyId;
@Column(name = "PRODUCT_ID")
@EqualsAndHashCode.Include
private int productId;
@Column(name = "CONFIG_ID")
@EqualsAndHashCode.Include
private String configId;
}
TB_CONFIG 表
@Getter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Entity
@Table(schema = "CLOUD_CATALOG", name = "TB_CONFIG")
@Cache(region = "oConfigurationsCache", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Configuration {
@EmbeddedId
private PkConfiguration id;
@Column(name = "CONFIG_VALUE")
private String configValue;
}
我的第一次尝试是映射@OneToOne 关系一个Configuration 类,如下所示:
具有 OneToOne 关系的 TB_CONFIG 表
@Getter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Entity
@Table(schema = "CLOUD_CATALOG", name = "TB_CONFIG")
@Cache(region = "oConfigurationsCache", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Configuration {
@EmbeddedId
private PkConfiguration id;
@Column(name = "CONFIG_VALUE")
private String configValue;
@OneToOne
@JoinColumn(name = "PRODUCT_ID", referencedColumnName = "PRODUCT_ID", insertable = false, updatable = false)
@JoinColumn(name = "CONFIG_ID", referencedColumnName = "CONFIG_ID", insertable = false, updatable = false)
private ConfigurationLanguage language;
}
然后像这样写一个 JPQL:
TypedQuery<ConfigurationCompany> query = manager.createQuery("select c from ConfigurationCompany c " +
"join c.language l " +
"where c.id.customerId = :customerId " +
"and c.id.companyId = :companyId " +
"and c.id.productId = :productId " +
"and c.id.configId = :configId " +
"and l.id.languageId = :languageId", ConfigurationCompany.class);
但我在尝试运行应用程序时收到以下异常:
org.springframework.orm.jpa.JpaSystemException: referencedColumnNames(PRODUCT_ID, CONFIG_ID) of com.mycompany.api.configuration.domain.model.ConfigurationCompany.language referencing com.mycompany.api.configuration.domain.model.ConfigurationLanguage not mapped to a single property; nested exception is org.hibernate.AnnotationException: referencedColumnNames(PRODUCT_ID, CONFIG_NAME) of com.mycompany.api.configuration.domain.model.ConfigurationCompany.language referencing com.mycompany.api.configuration.domain.model.ConfigurationLanguage not mapped to a single property
我认为这是因为我在创建 @OneToOne 映射时没有引用 Language_Id 列...
所以,问题是: 这是实现这种关系的正确方法吗?如果是,我错过了什么? 如果没有,我该如何正确配置此映射?
提示: 我无法更改数据库架构。
非常感谢!
【问题讨论】:
-
TB_CONFIG_LANG没有列CONFIG_NAME但您在@JoinColumn(name = "CONFIG_NAME", referenc...中使用它 -
@DirkDeyne,对不起...写问题时出错了...正确的名称是 CONFIG_ID
标签: hibernate jpa spring-data-jpa hibernate-mapping one-to-one