- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我这里有一个看起来像这样的图表,
可以在此 Answer Here 中找到.
+---------------+ +-------------------+
| PRODUCTS |-----< PRODUCT_VARIANTS |
+---------------+ +-------------------+
| #product_id | | #product_id |
| product_name | | #variant_id |
+---------------+ | sku_id |
| +-------------------+
| |
+--------^--------+ +--------^--------+
| PRODUCT_OPTIONS |-----< VARIANT_VALUES |
+-----------------+ +-----------------+
| #product_id | | #product_id |
| #option_id | | #variant_id |
+--------v--------+ | #option_id |
| | value_id |
+-----------------+ +--------v--------+
| OPTIONS | |
+-----------------+ |
| #option_id | |
| option_name | |
+-----------------+ |
| |
+-------^-------+ |
| OPTION_VALUES |---------------+
+---------------+
| #option_id |
| #value_id |
| value_name |
+---------------+
我设法在我的 Spring 应用程序中实现了整个图表,除了表
VARIANT_VALUES
问题在于设计建议表
PRODUCT_OPTIONS
,
OPTION_VALUES
和
PRODUCT_VARIANTS
有复合 Id(主键),我使用
@EmbeddedId
实现
//PRODUCT_VARIANTS
@Data
@NoArgsConstructor
@Entity
@Table(name = "product_variants")
public class ProductVariant {
@EmbeddedId
private ProductVariantId productVariantId;
@NotNull
private Double price;
@MapsId("productId")
@ManyToOne(fetch = FetchType.LAZY)
private Product product;
//other properties
}
和
//ProductVariantId
@Data
@Embeddable
public class ProductVariantId implements Serializable {
@EqualsAndHashCode.Include
private Long productVariantId;
@EqualsAndHashCode.Include
private Long productId;
}
现在我需要用
PRODUCT_OPTIONS
再次映射这三个表(
OPTION_VALUES
、
PRODUCT_VARIANTS
和
VARIANT_VALUES
)
//VariantValue
@Data
@NoArgsConstructor
@Entity
@Table(name = "variant_values")
public class VariantValue {
@EmbeddedId
private VariantValueId variantValueId;
@MapsId("productVariantId")
@ManyToOne(fetch = FetchType.LAZY)
private ProductVariant productVariant;
@MapsId("productOptionId")
@ManyToOne(fetch = FetchType.LAZY)
private ProductOption productOption;
@MapsId("optionValueId")
@ManyToOne(fetch = FetchType.LAZY)
private OptionValue optionValue;
}
//VariantValueId
@Embeddable
@Data
public class VariantValueId implements Serializable {
@EqualsAndHashCode.Include
@Embedded
private ProductVariantId productVariantId;
@EqualsAndHashCode.Include
@Embedded
private ProductOptionId productOptionId;
@EqualsAndHashCode.Include
@Embedded
private OptionValueId optionValueId;
}
但它最终创建了一个表,其中包含所有三个表中的每个 Id,
@Embdded
至
VariantValueId
它生成两列,我找不到任何方法来排除一个值并使用另一个值,
//variant_values
product_option_option_option_id [PK]
product_option_ptoduct_product_id [PK]
product_variant_ptoduct_product_id [PK]
product_variant_ptoduct_variant_id [PK]
product_value_option_option_id [PK]
product_value_option_value_id [PK]
那么我怎样才能实现这个只有
product_id [PK]
,
variant_id [PK]
,
option_id [PK]
和
value_id
正如设计所暗示的那样?
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "product_variants")
public class ProductVariant extends UserDateAudit {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "productVariantId", column = @Column(name = "variant_id")),
@AttributeOverride(name = "productId", column = @Column(name = "product_id"))
})
private ProductVariantId productVariantId;
@NotNull
private Double price;
//many to many with product entity, option entity and optionValue entity
@MapsId("productId")
@ManyToOne(fetch = FetchType.LAZY)
private Product product;
@OneToMany(mappedBy = "productVariant", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<VariantValue> variantValues;
}
@Data
@Embeddable
public class ProductVariantId implements Serializable {
private Long productVariantId;
private Long productId;
}
@Data
@NoArgsConstructor
@Entity
@Table(name = "variant_values")
public class VariantValue {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "productVariantId", column = @Column(name = "variant_id")),
@AttributeOverride(name = "productId", column = @Column(name = "product_id"))
@AttributeOverride(name = "optionId", column = @Column(name = "option_id")),
@AttributeOverride(name = "valueId", column = @Column(name = "value_id"))
})
private VariantValueId variantValueId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "variant_id", referencedColumnName = "variant_id", insertable = false, updatable = false),
@JoinColumn(name = "product_id", referencedColumnName = "product_id", insertable = false, updatable = false)
})
private ProductVariant productVariant;
//other mappings
我收到以下错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaAuditingHandler': Cannot resolve reference to bean 'jpaMappingContext' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to find column with logical name: product_id in product_variants
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:342) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:691) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:197) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1356) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1206) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:571) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925) ~[spring-context-5.3.1.jar:5.3.1]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588) ~[spring-context-5.3.1.jar:5.3.1]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.0.jar:2.4.0]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) ~[spring-boot-2.4.0.jar:2.4.0]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.4.0.jar:2.4.0]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.0.jar:2.4.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.0.jar:2.4.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) ~[spring-boot-2.4.0.jar:2.4.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) ~[spring-boot-2.4.0.jar:2.4.0]
at com.ecommerce.ECommerceApplication.main(ECommerceApplication.java:10) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.4.0.jar:2.4.0]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to find column with logical name: product_id in product_variants
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:609) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330) ~[spring-beans-5.3.1.jar:5.3.1]
... 27 common frames omitted
Caused by: org.hibernate.MappingException: Unable to find column with logical name: product_id in product_variants
at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:854) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:255) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:101) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processEndOfQueue(InFlightMetadataCollectorImpl.java:1823) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1767) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1655) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:286) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1224) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1255) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.1.jar:5.3.1]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.1.jar:5.3.1]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.1.jar:5.3.1]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
最佳答案
试试这样的:
@Data
@NoArgsConstructor
@Entity
@Table(name = "product_variants")
public class ProductVariant {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "productVariantId", column = @Column(name = "variant_id")),
@AttributeOverride(name = "productId", column = @Column(name = "product_id"))
})
private ProductVariantId productVariantId;
@NotNull
private Double price;
@MapsId("productId")
@ManyToOne(fetch = FetchType.LAZY)
private Product product;
//other properties
}
@Data
@Embeddable
public class ProductVariantId implements Serializable {
@EqualsAndHashCode.Include
private Long productVariantId;
@EqualsAndHashCode.Include
private Long productId;
}
@Data
@NoArgsConstructor
@Entity
@Table(name = "variant_values")
public class VariantValue {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "productVariantId", column = @Column(name = "variant_id")),
@AttributeOverride(name = "productId", column = @Column(name = "product_id")),
@AttributeOverride(name = "optionId", column = @Column(name = "option_id")),
@AttributeOverride(name = "valueId", column = @Column(name = "value_id"))
})
private VariantValueId variantValueId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "variant_id", referencedColumnName = "variant_id", insertable = false, updatable = false),
@JoinColumn(name = "product_id", referencedColumnName = "product_id", insertable = false, updatable = false)
})
private ProductVariant productVariant;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "option_id", referencedColumnName = "option_id", insertable = false, updatable = false),
@JoinColumn(name = "product_id", referencedColumnName = "product_id", insertable = false, updatable = false)
})
private ProductOption productOption;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "option_id", referencedColumnName = "option_id", insertable = false, updatable = false),
@JoinColumn(name = "value_id", referencedColumnName = "value_id", insertable = false, updatable = false)
})
private OptionValue optionValue;
}
@Embeddable
@Data
public class VariantValueId implements Serializable {
@EqualsAndHashCode.Include
private Long productVariantId;
@EqualsAndHashCode.Include
private Long productId;
@EqualsAndHashCode.Include
private Long optionId;
@EqualsAndHashCode.Include
private Long valueId;
}
编辑:
@Data
@NoArgsConstructor
@Entity
@Table(name = "product_variants")
public class ProductVariant {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "productVariantId", column = @Column(name = "variant_id")),
@AttributeOverride(name = "productId", column = @Column(name = "product_id"))
})
private ProductVariantId productVariantId;
@NotNull
private Double price;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColum(name = "product_id", insertable = false, updatable = false)
private Product product;
//other properties
}
关于java - 如何使用@EmbeddedId 在两个表之间进行映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64993785/
我有一个表,其中包含一个序列和两个外键的复合主键 我能够持久化我的实体类,但它没有按照顺序生成。由一个序列和两个外键组成的复合主键的表,maven中的hbm2java给出了以下实体 这是主要实体 pa
我有一个 hibernate 映射问题。我有以下两个数据库表(我不允许更改数据库): LOCATIONS { ID, -- PK NAME } LOCATION_GROUPS { L
我知道Hibernate中@EmbeddedId注解的复合主键至少由嵌入主键对象的所有对象变量的组合组成。 但是主键对象本身的引用是否也属于复合主键,因此它是由嵌入主键对象的引用值和对象变量构建的?
我对我的表格/实体有疑问。 我的 SQL 表如下所示: 表 1: IdTable1(pk) 属性... 表 2: IdTable2(pk) 属性... 表3: IdTable1(pk) IdTable
这是我的问题 IdComposite 类: @Embeddable public class IdComposite implements Serializable{ @Column(name =
我正在尝试使用引用另一个表的复合主键。我看到了很多主题,但没有答案适用于我的情况。 在我的数据库中,列是由未添加 owner_id 的外键正确创建的。因此,即使员工不存在,我也可以添加新记录。我使用
我收到了一个 Eclipse 错误:@EmbeddedId . 这是实体: @Entity @Table(name = "PERSON") public class Person implements
所以我这里有一个看起来像这样的图表, 可以在此 Answer Here 中找到. +---------------+ +-------------------+ | PRODUCTS
当我尝试从 JPA 存储库获取实体列表时,总是会遇到这样的异常 org.springframework.orm.jpa.JpaSystemException: No default construct
我正在尝试在一个类之间创建一对一的关系,我们将其称为“第一”,另一个类将其称为“第二”。 如果 Second 类使用 @EmbeddedId 作为其主键,它不会允许我这样做,我该如何解决这个问题? 基
我正在使用 hibernate,并且我已经使用 hibernate 在 java 端创建了实体。我的一个数据库表有多个列作为主键,hibernate 使用 @EmbeddedId 来处理这个问题。 我
我有一个主类和一个辅助类,用于存储保存时的错误。错误可以有多种类型,主键是错误的主要类别和类型。这是我的类的映射: 我的初级类(class): @Entity @Table(name = "foo")
我有一个具有复合主键的@Entity。因此,我创建了一个包含 PK 字段的 @EmbeddedId。 问题:是直接通过id.*访问这些字段更好,还是应该在父类中创建getter/setter? 例子:
我有一个包含三个字段 A、B、C 的 @Entity,其中 A 和 B 充当复合主键。我创建了一个包含 A 和 B 的 @EmbeddedId 类。为了减轻定义 getter 和 setter 的负担
在使用 hibernate 和 JPA 的 Spring MVC 应用程序中,我尝试为其基础数据表具有两列主键的实体设置映射。 如何更改下面的代码以使其正常工作? 我创建了一个名为 conceptPK
我有我的实体: @Entity @Table(name="performances") @AssociationOverrides({ @AssociationOverride(name="i
我有点麻烦。我正在使用 JPA Criteria 进行动态选择(使用标准作为 where 子句具有可选变量...)但我的实体之一有一个 EmbeddedId ,其中包含其中的用户列并且需要检查用户 I
我想使用条件进行以下查询。我有一个定义了 EmbeddedId 的 Entity: @Entity @Table(name="TB_INTERFASES") public class Inter
我的 @MapsId 注释和 @EmbeddedId 存在问题。在 Hibernate 中运行代码时,我得到: Caused by: org.hibernate.PropertyAccessExcep
我有以下实体: @Entity(name = "game_users") public class GameUser { private GameUsersPK primaryKey;
我是一名优秀的程序员,十分优秀!