我在使用Spring Data Rest編輯的資料庫中有一些物體。
他們都將這個作為基類:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
JsonSubTypes.Type(value = TextComponent::class, name = TextComponent.TYPE),
JsonSubTypes.Type(value = TextAreaComponent::class, name = TextAreaComponent.TYPE)
)
abstract class AbstractLabelComponent(
@field:ManyToOne
@field:JsonIgnore
open val template: Template?,
@field:Id
@field:GeneratedValue(strategy = GenerationType.AUTO)
open var id: Long? = null
)
這是一個中產階級(我不確定它是否重要,所以擁有它總比沒有好):
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
abstract class AbstractTextComponent(
template: Template?,
@field:ManyToOne(optional = false)
open var font: Font,
): AbstractLabelComponent(template)
這是一個兒童班。只發布一個,因為它們的行為方式都相同。
@Entity
class TextComponent(
template: Template? = null,
font: Font,
): AbstractTextComponent(template){
companion object {
const val TYPE = "text"
}
}
這是具有組件串列的模板:
@Entity
data class Template(
@field:OneToMany(mappedBy = "template", cascade = [CascadeType.ALL], orphanRemoval = true)
var components: MutableList<AbstractLabelComponent> = mutableListOf()
@field:Id
@field:GeneratedValue(strategy = GenerationType.AUTO)
open var id: Long? = null
)
我正在向 API 發送以下 JSON:
{
"components": [
{
"alignment": 0,
"content": "",
"fieldID": "TEXT",
"font": "/api/fonts/9",
"rotation": 0,
"type": "text",
"x": 84,
"y": 36
}
],
}
但我收到此錯誤:
JSON 決議錯誤:JSON 屬性字體的 [simple type, class com.components.TextComponent] 值的實體化失敗,因為創建者引數字體是不可為空型別的缺失值(因此為 NULL)
I figured it is because the font property in the constructor is not optional, so I made it optional. But then I received an error from the DB that font_id has no default value, so I think the real problem is that the font is not deserialized or passed properly.
Also weird that if I have the URI to the font in an array it complains that arrays can not be converted to Font, so I guess it can deserialize but it does not pass it to the constructor. Before that, I had it as a JSON of the Font but got the same error as now so I thought I should use URIs instead.
Here is the Font class:
@Entity
data class Font(
var humanReadable: String = "",
private val width: Int = 1,
private val height: Int = 1,
@field:Id
@field:GeneratedValue(strategy = GenerationType.AUTO)
open var id: Long? = null
)
I just tried with an other component that has no Font and got the same error that I mentioned that I get when the font is optional on my components:
java.sql.SQLException: Field 'bold_font_id' doesn't have a default value
Here is the other component that has no font:
@Entity
class CodeComponent(
template: Template? = null,
private var code: String = ""
): AbstractLabelComponent(template) {
這是一個具有bold_font 的組件。我在復制以下錯誤時沒有使用它:
@Entity
class TextAreaComponent(
template: Template? = null,
content: String = "",
font: Font,
@field:ManyToOne(optional = false)
var boldFont: Font
): AbstractTextComponent(template, font,content)
這是模板物體的存盤庫:
@Repository
interface TemplateRepository: CrudRepository<Template, Long>{
}
uj5u.com熱心網友回復:
在我看來bold_font_id,您的資料庫表中有一個列,但我在您的模型類中看不到任何映射到此類列的屬性。確保您有一個與資料庫表中的每一列匹配的屬性,否則,您必須在創建資料庫模式時在資料庫模式中分配默認值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/373349.html
