S
S
Splite2017-05-31 16:19:13
Java
Splite, 2017-05-31 16:19:13

How to work with links in Ebean ORM?

Hello, I just can't figure out how to work with one-to-many relationships in Ebean ORM. Suppose there are such entities

@Entity
open class Member() {
    @ManyToOne(optional = false)
    open var group: Group? = null
}

@Entity
open class Group() {
    @OneToMany(mappedBy = "group", fetch = FetchType.EAGER, cascade = arrayOf(CascadeType.ALL))
    open var members: MutableList<Member> = ArrayList()
}

I want to add new Member for some Group like this
//группа в переменной gr
val memb: Member = Member("testName")
memb.group = gr
gr.members.add(memb)
ebeanServer.save(gr)

Everything seems to be ok, but if I do
ebeanServer.save(gr)
it again, regardless of what changes I made there, I get an exception
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '494' for key 'PRIMARY'

ID 494 refers specifically to the new memb member, ebean is trying to save it again
. What could be the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question