Hello,
I have these two tables in my DB model:
Code:
create table "LANGUAGE_TEXTS" (
"TEXT_ID" number(10,0),
"LANGUAGE_NAME" varchar2(2 byte),
"TEXT" varchar2(4000 char),
constraint "PK_LANGUAGE_TEXTS" primary key ("TEXT_ID", "LANGUAGE_NAME"),
constraint "FK_LANG_TEXTS$TEXTS" foreign key ("TEXT_ID") references "TEXTS" ("TEXT_ID")
);
create table "TEXTS" (
"TEXT_ID" number(10,0),
constraint "PK_TEXTS" primary key ("TEXT_ID")
);
create table "USER_ROLES" (
"ROLE_NAME" varchar2(50 byte),
"TEXT_ID" number(10,0),
constraint "FK_USER_ROLES$TEXTS" foreign key ("TEXT_ID") references "TEXTS" ("TEXT_ID")
);
The first table holds localised texts for all locale specific objects in the application. The second one helps to associate the texts with the objects using TEXT_ID foreign key (because the LANGUAGE_TEXT table has composite primary key). The USER_ROLES table illustrates using of language texts. Note that TEXT_ID must be generated using Oracle sequence.
So I designed the following base of corresponding domain model entities:
Code:
@Entity
@Table (name = "LANGUAGE_TEXTS")
public class LanguageText {
@EmbeddedId
private LanguageTextPK id;
@Column (name = "TEXT")
private String text;
...
}
@Embeddable
public class LanguageTextPK {
@Column(name = "TEXT_ID", nullable = false)
private Long id;
@Column(name = "LANGUAGE_NAME", nullable = false)
private String languageName;
...
}
@Entity
@Table(name = "TEXTS")
@SequenceGenerator(name = "textIdGenerator", sequenceName = "SEQ_TEXTS")
public class Text {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "textIdGenerator")
@Column(name = "TEXT_ID", updatable = false)
private Long id;
...
}
@Entity
@Table(name = "USER_ROLES")
public class UserRole {
@Id
@Column(name = "ROLE_NAME")
private String name;
@Column(name = "TEXT_ID")
private Long textId;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "TEXT_ID", referencedColumnName = "TEXT_ID")
@MapKey(name = "id.languageName")
private Map<String, LanguageText> languageTexts;
...
}
Now, I need associate LanguageText and Text entities so that all will be correctly saved to database. I want only call session.save(userRole), that's all. But I can't find a way how to make it...
Note that the DB model is legacy and I can't change it.
Thank you very much for any suggestions.
Darbic
---
Hibernate version: 3.2.0
Name and version of the database you are using: Oracle Database 10.2 XE
JDBC driver: Oracle Thin JDBC Driver 10.1.0.5.0