我有以下模型类:
@Entity
@Inheritance
public abstract class BasePage {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@MapKey(name="locale")
private Map<String, Localized> text = new HashMap<String, Localized>();
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@MapKey(name="locale")
private Map<String, Localized> title = new HashMap<String, Localized>();
// ....
}
@Entity
public class Localized {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String locale;
@Lob
@Type(type="org.hibernate.type.StringClobType")
private String text;
@Lob
@Type(type="org.hibernate.type.StringClobType")
private String synopsis;
// ....
}
@Entity
@DiscriminatorValue("FLATPAGE")
public class FlatPage {
}
@Entity
@DiscriminatorValue("INSTITUTION")
public class Institution {
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@MapKey(name="locale")
private Map<String, Localized> contact;
}
到目前为止,我有一个小的Entity类来保存本地化(HTML)字符串和一个抽象类,它是我的应用程序中特定类型的节点/文章的基础。
为实体生成的SQL看起来不错,但是我将表BasePage
实体映射到Localized
个实体的表存在问题。
使用Hibernate(4.3.4)和PostgreSQL(9.1),生成以下模式:
CREATE TABLE basepage_localized
(
basepage_id BIGINT NOT NULL,
contact_id BIGINT NOT NULL,
synopsis_id BIGINT NOT NULL,
text_id BIGINT NOT NULL,
PRIMARY KEY ( basepage_id, synopsis_id ),
FOREIGN KEY ( basepage_id ) REFERENCES basepage ( id ),
FOREIGN KEY ( text_id ) REFERENCES localized ( id ),
FOREIGN KEY ( contact_id ) REFERENCES localized ( id ),
FOREIGN KEY ( synopsis_id ) REFERENCES localized ( id )
);
问题在于我无法创建FlatPage
个实体,因为contact_id
具有NOT NULL
约束。我怀疑我的架构可能会有更多问题,但那是我目前正在努力解决的问题......