If the name attribute of @JoinTable is not specified, join table is not
created for entity relationship. E.g.
@Entity public class LockScopeEntity1xm {
@OneToMany(mappedBy="ownerOne")
private Collection<LockScopeEntityMany> ownedMany = new
HashSet<LockScopeEntityMany>();
@Entity public class LockScopeEntityMany {
@JoinTable(name="xxx")
@ManyToOne
private LockScopeEntity1xm ownerOne;
If name="xxx" is not specified, the following mapping is created:
CREATE TABLE LockScopeEntity1xm (id INTEGER NOT NULL, version INTEGER,
PRIMARY KEY (id))
CREATE TABLE LockScopeEntityMany (id INTEGER NOT NULL, version INTEGER,
OWNERONE_ID INTEGER, PRIMARY KEY (id))
CREATE INDEX I_LCKSMNY_OWNERONE ON LockScopeEntityMany (OWNERONE_ID)
Otherwise:
CREATE TABLE LockScopeEntity1xm (id INTEGER NOT NULL, version INTEGER,
PRIMARY KEY (id))
CREATE TABLE LockScopeEntityMany (id INTEGER NOT NULL, version INTEGER,
PRIMARY KEY (id))
CREATE TABLE xxx (OWNEDMANY_ID INTEGER, OWNERONE_ID INTEGER)
CREATE INDEX I_XXX_OWNEDMANY_ID ON xxx (OWNEDMANY_ID)
CREATE INDEX I_XXX_OWNERONE ON xxx (OWNERONE_ID)
However the spec spelled out the default in 11.1.23 that:
@Target({METHOD, FIELD}) @Retention(RUNTIME)
public @interface JoinTable {
String name() default "";
...... }
Table 22 JoinTable Annotation Elements
String | name | (Optional) The name of the join table. | Default: The
concatenated names of the two associated primary entity tables (owning side
first), separated by an underscore.
Shouldn't in the case where name="xxx" is not specified the following
mapping should be created?
CREATE TABLE OWNEDMANY_OWNERONE (OWNEDMANY_ID INTEGER, OWNERONE_ID INTEGER)
The setting of the join table name is in AnnotationPersisteneMappingParser
private void parseJoinTable(FieldMapping fm, JoinTable join) {
FieldMappingInfo info = fm.getMappingInfo();
info.setTableName(toTableName(join.schema(), join.name()));
private static String toTableName(String schema, String table) {
if (StringUtils.isEmpty(table))
return null;
if (StringUtils.isEmpty(schema))
return table;
return schema + "." + table;
}
If name is not specified, info.setTableName is set to null, that means no
join table is used.
Is this interpretation is correct?
--
Albert Lee.