-2
I have a question about how to associate two entities using spring data + spring boot.
I have both relationships:
@Entity(name = "profiles")
class Profile(
@field:Id
@field:GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long?,
@field:NotNull
@field:Size(min = 5, max = 50)
var name: String?,
@field:NotNull
@field:Column(unique = true)
@field:Size(max = 120)
var contactEmail: String?,
@field:NotNull
@field:Column(unique = true) @Size(max = 15)
var contactCellphone: String?,
@field:Column(nullable = false)
var createdAt: Date?,
@field:Column(nullable = false)
var updatedAt: Date?,
@JsonIgnore()
@OneToMany(mappedBy = "profile", cascade = arrayOf(CascadeType.ALL), fetch = FetchType.LAZY)
var debits: List = emptyList()
) {
@PrePersist
fun prePersist() {
this.createdAt = Date();
this.updatedAt = Date();
}
@PreUpdate
fun preUpdate() {
this.updatedAt = Date()
}
}
@Entity(name = "debits")
data class Debit(
@field:Id
@field:GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long?,
@field:NotNull
@field:Digits(integer = 5, fraction = 2)
@field:Min(1)
var debits: BigDecimal?,
@field:NotNull
@field:Enumerated(EnumType.STRING)
var status: DebitStatus = DebitStatus.OPENED,
@field:Column(nullable = false)
var createdAt: Date?,
@field:Column(nullable = false)
var updatedAt: Date?,
@JsonIgnore()
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "profile_id", nullable = false)
val profile: Profile? = null,
@JsonIgnore()
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "notification_rule_id")
val notificationRule: NotificationRule? = null
) {
@PrePersist
fun prePersist() {
this.createdAt = Date();
this.updatedAt = Date();
}
@PreUpdate
fun preUpdate() {
this.updatedAt = Date()
}
}
enum class DebitStatus {
OPENED, CLOSED, DISABLED
}
@RestController
@RequestMapping("/debits")
class DebitController {
@Autowired
private val repository: DebitRepository? = null
@GetMapping
fun index(): List {
return repository!!.findAll().toList()
}
@GetMapping("/{id}")
fun show(@PathVariable("id") debit: Debit): Debit {
return debit;
}
@PostMapping
fun create(@Valid @RequestBody debit: Debit?): Debit {
return repository!!.save(debit!!)
}
@PutMapping("/{id}")
fun update(@PathVariable(value = "id") debit: Debit, @Valid @RequestBody newDebit: Debit): Debit {
debit.apply {
this.debits = newDebit.debits ?: this.debits
this.status = newDebit.status ?: this.status
}
return repository!!.save(debit)
}
}
After creating the profile i am trying to create the debit by already associating the debit in an existing profile. But it gives an error:
{
"debits": 100.39,
"profile": 1
}
And the mistake:
{
"timestamp": "2020-06-21T18:05:43.316+00:00",
"status": 500,
"error": "Internal Server Error",
"trace": "Caused by: [... um monte de log inútil pra sua pergunta], org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column \"PROFILE_ID\"; SQL statement:insert into debits (id, created_at, debits, notification_rule_id, profile_id, status, updated_at) values (null, ?, ?, ?, ?, ?, ?) [23502-200]",
"message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
"path": "/debits"
}
Is there a possibility to create and already associate an entity in the other? If I can do it using spring??
If I understand your question, I don’t think you can automatically. You will have, via constructor or Setter itself, to inform the entity
Debitwhich theProfileassociated, searching the database and then passing it to the entityDebit. Ah, next time, do an edit on that stacktrace. 99% of it is useless to your problem. Help us help you.– StatelessDev