【发布时间】:2013-11-06 04:56:25
【问题描述】:
我正在尝试让 Hibernate (v 4.2.3) 在应用程序启动时验证 (hbm2ddl.auto = validate) 我现有的 4 个数据库表。这是我的表创建 SQL 脚本(这是一个 H2 DB):
-- Lookup/reference table, example records might be for ADVERB, NOUN,
-- VERB, etc.
CREATE TABLE word_types (
word_type_id BIGINT AUTO_INCREMENT,
word_type_label VARCHAR(100) NOT NULL,
word_type_description VARCHAR(100) NOT NULL,
word_type_tag VARCHAR(100) NOT NULL,
CONSTRAINT uc_tag UNIQUE (word_type_tag)
);
-- A word in the English language. length is the number of chars in the
-- word, type ID is the word_types#word_type_id above (foreign key),
-- text is the actual word itself "quick", "fast", etc.
CREATE TABLE words (
word_id BIGINT AUTO_INCREMENT,
word_length INTEGER NOT NULL,
word_type_id INTEGER NOT NULL,
word_text VARCHAR(100) NOT NULL,
word_definition VARCHAR(1000) NOT NULL,
CONSTRAINT fk_word_types FOREIGN KEY (word_type_id) REFERENCES word_types(word_type_id),
CONSTRAINT uc_text_type UNIQUE (word_text, word_type_id)
);
-- Crosswalk/junction table holding a many-to-many relationships between
-- pairs of words. Example: fast is a synonym of quick. So there would be
-- a words record for fast, and a words record for quick, and a record in
-- this table linking the 2 together.
CREATE TABLE synonyms (
synonym_id BIGINT AUTO_INCREMENT,
base_word_id INTEGER NOT NULL,
has_synonym_id INTEGER NOT NULL,
CONSTRAINT fk_word_1_base_id FOREIGN KEY (base_word_id) REFERENCES words(word_id),
CONSTRAINT fk_word_synonym_id FOREIGN KEY (has_synonym_id) REFERENCES words(word_id),
CONSTRAINT uc_syn_id_sets UNIQUE (base_word_id, has_synonym_id)
);
-- Same as above except this table relates words that are antonyms of
-- each other.
CREATE TABLE antonyms (
antonym_id BIGINT AUTO_INCREMENT,
base_word_id INTEGER NOT NULL,
has_antonym_id INTEGER NOT NULL,
CONSTRAINT fk_word_2_base_id FOREIGN KEY (base_word_id) REFERENCES words(word_id),
CONSTRAINT fk_word_antonym_id FOREIGN KEY (has_antonym_id) REFERENCES words(word_id),
CONSTRAINT uc_ant_id_sets UNIQUE (base_word_id, has_antonym_id)
);
因此,有 4 个表:words、synonyms 和 antonyms(在不同的 words 之间保持多对多关系)和一个查找/引用表 word_types(例如 ADVERB、NOUN、等等。)。澄清一下,如果有一个words 记录的word_text 值为“快速”,另一个words/word_text 记录/值为“快速”,那么synonyms 中可能有一个条目base_word_id 是“快速”的 ID,has_synonym_id 可能是“快速”的 ID 的表;因为quick有一个同义词叫做fast。这是我要用于这些表的 Java 模型:
public class BaseModel {
protected Long id;
public Long getId() {
return id;
}
public void setId(final Long id) {
this.id = id;
}
}
public class Word extends BaseModel {
private String text;
private Integer length;
private WordType type;
private String definition;
private List<Word> synonyms;
private List<Word> antonyms;
// Getters, setters, ctors omitted for brevity...
}
public class BaseLookup extends BaseModel {
private String label;
private String description;
private String tag;
// Getters, setters, ctors omitted for brevity...
}
public class WordType extends BaseLookup {
public WordType(String label, String description, String tag) {
super(label, description, tag);
}
}
所以BaseModel 为每个模型提供了一个 ID。 BaseLookup 至少提供所有查找表都将具有的三个字段/列。 Word 非常简单,WordType 是一个查找包装器,它不会在其父级上添加任何其他字段。然而,有朝一日有一个BaseLookup 子类确实在BaseLookup 提供的标签/描述/标签字段之外添加字段是非常可能的。
所以我试图弄清楚我需要为我的每个类添加哪些注释,以便正确配置 Hibernate 以使用我的 Java 和数据模型,但我遇到了一些砖墙。这是我能想到的最好的:
// This class doesn't translate into a table; it's just a base class that provides
// an ID for all other entities, and perhaps (down the road) other common fields as
// well.
public class BaseModel {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
protected Long id;
public Long getId() {
return id;
}
public void setId(final Long id) {
this.id = id;
}
}
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name="words")
public class Word extends BaseModel {
// How do I force Word.getId() to be "words_id"?
@Column(name="word_text")
private String text;
@Column(name="word_length")
private Integer length;
// But how do I make this the ID of a word_types record?
@Column(name="word_type_id")
private WordType type;
@Column(name="word_definition")
private String definition;
// The words table doesn't have any synonyms or antonyms.
// Rather there is a many-to-many relationship between
// a word and its synonyms and its antonyms...
@Column(name="???")
private List<Word> synonyms;
@Column(name="???")
private List<Word> antonyms;
// Getters, setters, ctors omitted for brevity...
}
// Not sure what to annotate this table with, because there is not
// base_lookup table or anything like that...
public class BaseLookup extends BaseModel {
private String label;
private String description;
private String tag;
// Getters, setters, ctors omitted for brevity...
}
// Furthermore, here, in the case of WordType, I'd like to force the parent
// fields to be "word_type_label", "word_type_description", and "word_type_tag";
// however, other BaseLookup subclasses should be able to force those same fields
// to map/bind to other tables with other field names.
//
// For example, I might some day want a Color POJO relating to a colors table with
// the following fields: color_label, color_description and color_tag, etc.
public class WordType extends BaseLookup {
// How do I force WordType.getId() to be word_type_id?
public WordType(String label, String description, String tag) {
super(label, description, tag);
}
}
能否请一些厌倦了 Hibernate 的老手帮助我正确地注释我的 POJO 类/字段,以便 Hibernate 能够同时适应我的 Java 和数据模型?具体来说,我需要以下解决方案:
- 如何使
BaseModel#id成为所有其他实体的 ID,但显示为具有每个实体的唯一列名的唯一列(word_id、word_type_id、color_id` 等)。 - 如何注释
Word#type字段以便Hibernate 知道它是word_type_id外键。此外,我需要以这样一种方式进行级联工作,即当我从数据库中获取WordPOJO 实例时,它已经填充了其WordType类型。 - 如何注释
Word#synonyms和Word#antonyms以便 Hibernate 将它们的关系存储在人行横道表中(同名)。 - 如何注释
WordType和BaseLookup以便Hibernate 知道查找名为word_types的表,其中包含以下字段:word_type_label、word_type_description和word_type_tag。 但是,请以这样的方式对它们进行注释,以便我也可以拥有其他BaseLookup子类,例如Color可能与带有color_label、color_description和 @987654363 的colors表相关@。
提前致谢!
【问题讨论】:
-
关系数据库表名不应该是复数,除非单行代表多个事物,如果这样做将是一个糟糕的关系设计。
-
我不同意;没有比我说“Jarrod”应该拼写“Jared”更合乎逻辑的了。而在 Java 模型中,将 POJO 称为“
Word”而不是“Words”是有意义的(因为,当您处理 POJO 的一个实例时,您正在处理一个单词)。但是表格代表了一个记录的集合,这就是我将它复数的原因。
标签: java sql hibernate jpa data-modeling