Hi, I broke my head, I can figure out what do I do wrong.
I have two entities with unidirectional ManyToMany relation.
Command has many jobs.
Job doesn't know anything about Command.
Same job can belong to different commands.
I can save single Job.
I can save single Command (w/o jobs)
Command with job is not saved correctly.
Command table gets record.
Reference doesn't get record.Job table gets record.
here is code
Code:
@Entity
@DynamicInsert
@DynamicUpdate
@SelectBeforeUpdate
@Table
class Job extends AbstractEntity {
// some regular fields go here.
}
@Entity
@DynamicInsert
@DynamicUpdate
@SelectBeforeUpdate
@Table
class Command extends AbstractEntity {
// some regular fields go here.
@ManyToMany(fetch = FetchType.EAGER, cascade = Array(CascadeType.ALL))
@JoinTable(name = "CommandJobs",
joinColumns = Array(new JoinColumn(name = "CommandId", nullable = false)),
inverseJoinColumns = Array(new JoinColumn(name = "JobId", nullable = false)))
var jobs: java.util.Set[Job] = _
def addJobs(job: Job): Unit = {
if (jobs == null) {
jobs = new util.HashSet[Job]()
}
jobs.add(job)
}
}
I try to persist command and I see that Hibernate does three SQL queries (SQL debug enabled):
1. insert into Command
2. Insert into Job
Where is insert into CommandJobs?