У меня есть две таблицы PostgreSQL: одна для Продукт и еще один для изображений галереи с подробностями SEO. Вот схемы:
Код: Выделить всё
create table if not exists test_product
(
id bigserial not null,
version bigserial not null,
name text not null,
sku varchar(50) not null,
primary key (id)
);
create table if not exists test_product_image_gallery_seo
(
id bigserial not null,
version bigserial not null,
product_id bigint not null,
image text not null,
alt_text text not null,
primary key (id),
constraint fk_product_id foreign key (product_id) references test_product (id),
constraint test_unique_seo unique (product_id, image)
);
TestProduct
Код: Выделить всё
@Entity(name = "test_product")
@Table(name = "test_product")
public class TestProduct extends BehemothORM {
@Column(
name = ProductContract.NAME,
nullable = false,
unique = true,
columnDefinition = "TEXT"
)
private String name;
@Column(
name = ProductContract.SKU,
nullable = false,
unique = true,
columnDefinition = "VARCHAR"
)
private String sku;
@OneToMany(
mappedBy = "product",
fetch = FetchType.EAGER,
targetEntity = TestProductImageGallerySEO.class,
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List imageGallerySEOList;
}
Код: Выделить всё
@Entity(name = "test_product_gallery_image_seo")
@Table(
name = "test_product_gallery_image_seo",
uniqueConstraints = {
@UniqueConstraint(
name = "test_unique_seo",
columnNames = {
ProductImageGallerySEOContract.PRODUCT_ID,
ProductImageGallerySEOContract.IMAGE
}
)
}
)
public class TestProductImageGallerySEO extends BehemothORM {
@Column(
name = "image",
columnDefinition = "TEXT",
nullable = false
)
private String image;
@Column(
name = "alt_text",
columnDefinition = "TEXT",
nullable = false
)
private String altText;
@ToString.Exclude
@ManyToOne(
targetEntity = TestProduct.class,
fetch = FetchType.EAGER
)
@JoinColumn(
name = ProductImageGallerySEOContract.PRODUCT_ID,
columnDefinition = "BIGSERIAL",
nullable = false,
foreignKey = @ForeignKey(
name = "fk_product_id"
)
)
@GsonExclude
private TestProduct product;
@Transient
private boolean deleted = false;
}
Код: Выделить всё
@Transactional()
public int modifyGalleryImages(@NotNull TestProductImageGallerySEOPayload seoPayload) {
// gets the current product as entity from database
TestProduct product = this.testProductDAOController.retrieveEntity(seoPayload.getProductId());
for (TestProductImageGallerySEO imageGallerySEO : seoPayload.getGallerySEOList())
{
imageGallerySEO.setProduct(product);
this.addNewEntity(imageGallerySEO);
}
return ActionCode.UPDATE_SUCCESS;
}
После обновления Hibernate с версии 6.3.1 до 6.6.3 я' получаю следующую ошибку:
Код: Выделить всё
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.bloomscorp.loom.seo.orm.TestProductImageGallerySEO#0]
Код: Выделить всё
{
"productId": 37503,
"gallerySEOList": [
{
"id": 0,
"version": 0,
"image": "uvw.jpg",
"altText": "",
"deleted": false
},
{
"id": 0,
"version": 0,
"image": "https://abc.jpg",
"altText": "",
"deleted": false
},
{
"id": 0,
"version": 0,
"image": "https://xyz.jpg",
"altText": "",
"deleted": false
}
]
}
если кто-то хочет знать, что делают BehemothORM и addNewEntity , это по сути наш собственный набор библиотек, которые мы используем для ускорения процесса разработки. Вот определение этих двух
BehemothORM
Код: Выделить всё
BehemothORMaddNewEntity
Код: Выделить всё
addNewEntityВопросы:
- Почему эта ошибка возникает сейчас после обновления Hibernate?
- Как исправить эту ошибку, сохранив текущую логику?
Подробнее здесь: https://stackoverflow.com/questions/792 ... updated-or
Мобильная версия