Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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
Spring boot OneToOne关系问题_Spring_Spring Boot_Jpa_Kotlin_Spring Data Jpa - Fatal编程技术网

Spring boot OneToOne关系问题

Spring boot OneToOne关系问题,spring,spring-boot,jpa,kotlin,spring-data-jpa,Spring,Spring Boot,Jpa,Kotlin,Spring Data Jpa,我需要从数据库中获取数据,但不包含某些字段,但我接收或删除了所有数据(带有关系)或错误 import org.springframework.data.domain.Persistable import org.springframework.data.util.ProxyUtils import java.io.Serializable import javax.persistence.GeneratedValue import javax.persistence.Id import java

我需要从数据库中获取数据,但不包含某些字段,但我接收或删除了所有数据(带有关系)或错误

import org.springframework.data.domain.Persistable
import org.springframework.data.util.ProxyUtils
import java.io.Serializable
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.MappedSuperclass
import javax.persistence.Transient

@MappedSuperclass
abstract class AbstractEntity<T : Serializable>() : Persistable<T> {

    companion object {
        private val serialVersionUID = -5554308939380869754L
    }

    @Id
    @GeneratedValue
    private var id: T? = null

    override fun getId(): T? {
        return id
    }

    fun setId(id:T){
        this.id = id
    }

    /**
     * Must be [Transient] in order to ensure that no JPA provider complains because of a missing setter.
     *
     * @see org.springframework.data.domain.Persistable.isNew
     */
    @Transient
    override fun isNew() = null == getId()

    override fun toString() = "Entity of type ${this.javaClass.name} with id: $id"

    override fun equals(other: Any?): Boolean {
        other ?: return false

        if (this === other) return true

        if (javaClass != ProxyUtils.getUserClass(other)) return false

        other as AbstractEntity<*>

        return if (null == this.getId()) false else this.getId() == other.getId()
    }

    override fun hashCode(): Int {
        return 31
    }
}
我知道这是通过@OneToOne发生的,但我无法寻求解决方案;(

我只需要接收一些字段,如:产品名称、描述、产品模型、产品文章、产品计数


谢谢你们的帮助,伙计们!

@OneToOne
如果您允许
null
值,这将是一个棘手的问题

JPA必须尝试访问另一个表中的记录,即使该属性是用延迟初始化签名的,因为它必须决定是将该属性设置为
null
还是代理对象

它无法自动将其设置为
null
,因为可能存在关联的对象。 它也无法自动创建代理对象,因为表中没有为该属性的id指定的列(因为默认情况下,一对一关系由共享PK映射)

因此,确定关联实体是否存在的唯一方法是尝试从其自己的表中加载其
id
。如果此查询返回
null
,则没有关联实体,否则JPA可以从此id创建代理对象

如果确实需要避免此额外请求,请将
product\u category\u属性
表的外键添加到
product
表中

@OneToOne(cascade = [CascadeType.PERSIST, CascadeType.MERGE], orphanRemoval = false, fetch = FetchType.LAZY)
@JoinColumn(name='product_category_property')
var productCategoryProperty: ProductCategoryPropertyEntity = ProductCategoryPropertyEntity()
你可以找到进一步的解释

import ua.jdroidcoder.jdcshop.dto.ProductCategoryPropertyDto
import javax.persistence.*

@Entity
@Table(name = "product_category_property")
class ProductCategoryPropertyEntity : AbstractEntity<Long> {

    @OneToMany(cascade = [CascadeType.PERSIST, CascadeType.MERGE], orphanRemoval = false)
    var simpleProperties: MutableList<ProductPropertySimpleEntity> = ArrayList()

    @OneToMany(cascade = [CascadeType.PERSIST, CascadeType.MERGE], orphanRemoval = false)
    var advancedProperties: MutableList<ProductPropertySimpleEntity> = ArrayList()

    constructor() : super() {
    }

    fun convertToDto(): ProductCategoryPropertyDto {
        val simple = ProductCategoryPropertyDto()
        simple.id = id
        simpleProperties?.forEach {
            simple.simpleProperties.add(it.convertToDto())
        }
        advancedProperties?.forEach {
            simple.advancedProperties.add(it.convertToDto())
        }
        return simple
    }
}
import ua.jdroidcoder.jdcshop.dto.PropertySimpleDto
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Table

@Entity
@Table(name = "product_property_simple")
class ProductPropertySimpleEntity : AbstractEntity<Long> {
    @Column(name = "property_name")
    var propertyName: String = ""

    @Column(name = "property_value")
    var propertyValue: String = ""

    @Column(name = "parent_id")
    var parentId: Long? = null

    @Column(name = "parent_id_in_category")
    var parentIdInCategory: Long? = null

    @Column(name = "id_in_category")
    var idInCategory: Long? = null

    constructor() : super() {
    }

    fun convertToDto(): PropertySimpleDto {
       return PropertySimpleDto(id!!,propertyName,parentId,propertyValue)
    }
}
Hibernate: select productent0_.id as id1_3_, productent0_.category_id as categor13_3_, productent0_.description as descript2_3_, productent0_.images as images3_3_, productent0_.is_available as is_avail4_3_, productent0_.keywords as keywords5_3_, productent0_.product_articul as product_6_3_, productent0_.product_category_property_id as product14_3_, productent0_.product_count as product_7_3_, productent0_.product_extra as product_8_3_, productent0_.product_model as product_9_3_, productent0_.product_name as product10_3_, productent0_.product_price as product11_3_, productent0_.product_total_price as product12_3_ from product productent0_
Hibernate: select categoryen0_.id as id1_0_0_, categoryen0_.category_name as category2_0_0_, categoryen0_.prom_category_id as prom_cat3_0_0_ from category categoryen0_ where categoryen0_.id=?
Hibernate: select productcat0_.id as id1_4_0_ from product_category_property productcat0_ where productcat0_.id=?
Hibernate: select simpleprop0_.product_category_property_entity_id as product_1_6_0_, simpleprop0_.simple_properties_id as simple_p2_6_0_, productpro1_.id as id1_7_1_, productpro1_.id_in_category as id_in_ca2_7_1_, productpro1_.parent_id as parent_i3_7_1_, productpro1_.parent_id_in_category as parent_i4_7_1_, productpro1_.property_name as property5_7_1_, productpro1_.property_value as property6_7_1_ from product_category_property_simple_properties simpleprop0_ inner join product_property_simple productpro1_ on simpleprop0_.simple_properties_id=productpro1_.id where simpleprop0_.product_category_property_entity_id=?
Hibernate: select advancedpr0_.product_category_property_entity_id as product_1_5_0_, advancedpr0_.advanced_properties_id as advanced2_5_0_, productpro1_.id as id1_7_1_, productpro1_.id_in_category as id_in_ca2_7_1_, productpro1_.parent_id as parent_i3_7_1_, productpro1_.parent_id_in_category as parent_i4_7_1_, productpro1_.property_name as property5_7_1_, productpro1_.property_value as property6_7_1_ from product_category_property_advanced_properties advancedpr0_ inner join product_property_simple productpro1_ on advancedpr0_.advanced_properties_id=productpro1_.id where advancedpr0_.product_category_property_entity_id=?
@OneToOne(cascade = [CascadeType.PERSIST, CascadeType.MERGE], orphanRemoval = false, fetch = FetchType.LAZY)
@JoinColumn(name='product_category_property')
var productCategoryProperty: ProductCategoryPropertyEntity = ProductCategoryPropertyEntity()