Java 休眠 4 @OneToMany List<String>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19966486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate 4 @OneToMany List<String>
提问by fxe
For a @OneToMany relationship do I have to map to Objects ?
对于 @OneToMany 关系,我是否必须映射到 Objects ?
I have the following example
我有以下例子
@Entity
@Table(name="FOO_TABLE")
public class Foo {
@Id
@Column(name="ID")
@GeneratedValue
private int id;
?????
private List<String> bars;
}
If bars were an object i would do
如果酒吧是一个对象,我会做
@ManyToOne
@JoinColumn(name="ID_FOO")
and the problem would be solved. However, I want avoid to create an object to only represent a pair of strings (reference key, value).
问题就解决了。但是,我想避免创建一个仅表示一对字符串(引用键、值)的对象。
FOO and BAR are stored in separate tables
FOO 和 BAR 存储在单独的表中
CREATE TABLE Foo (
ID INTEGER NOT NULL AUTO_INCREMENT,
//SOME OTHER PROPERTIES
PRIMARY KEY( ID),
);
CREATE TABLE Bar (
ID_FOO INTEGER,
VAL VARCHAR(256),
PRIMARY KEY (ID_FOO, VAL),
FOREIGN KEY ( ID_FOO) REFERENCES Foo( ID) ON DELETE CASCADE
);
采纳答案by Alan Hay
@ElementCollection
is what you are looking for. This lets you define a mapping for a non-Entity class e.g. Embeddable or Basic.
@ElementCollection
就是你要找的。这让您可以为非实体类定义映射,例如 Embeddable 或 Basic。
http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection
http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection
You can also use @CollectionTable to define the table.
您还可以使用@CollectionTable 来定义表。
@ElementCollection
@CollectionTable(name = "data" ....)
private List<String> data;