I'am struggling to get Hibernate (with MySQL) to generate the primary key for a reference table from a "main table". My problem is that I got a big table with 25 mil rows and now I need to add multiple additional columns and because in the future where will be even more columns to add I choose the way to work with reference tables instead of adding the columns to the main table (the rebuild takes hours... :)
So there is a main table and a reference table. In my conception the primary key of the reference table should be generated from the primary key of the main table. I could first insert
an entry into the main table, then select
it and use its primary key for the insert
into the reference table, but this seems not to be the best way to me. So I would like to use Hiibernate's generators
, but I can't figure out how.
Here's the main table:
@Entity
@Table
public class Task {
@Id
@GeneratedValue
@Column()
private Integer id;
// ...
@OneToOne(mappedBy = "task_ref", orphanRemoval=true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Stuff stuff;
// ...
}
And the reference table:
@Entity
@Table
public class Stuff {
@Id
@Column(name = "stuff_id")
@GeneratedValue()
private Integer stuff_id;
// ...
@OneToOne(orphanRemoval=true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private Task task_ref;
// ...
}
So.. how can I use a generator to create the primary key for the table Stuff
from the primary key of the table Task
?
Any suggestions or other solutions are highly welcome!
Thanks!