Hibernate:如何持久化包含Map的类<;字符串,列表<;字符串>&燃气轮机;
我想持久化以下类,包括“bar” Class Foo { Map<String, List<String>> bar; // ... // other stuff..... } 福班 { 地图栏; // ... //其他东西。。。。。 } 在Hibernate中如何执行此操作? 如果可能,如何在带注释的Hibernate中实现这一点 哦,映射键字符串的本质是它们的数量很少(5-40个),并且它们在Foo实例中是相同的。列表中的字符串在列表内和Foo实例之间都是唯一的Hibernate:如何持久化包含Map的类<;字符串,列表<;字符串>&燃气轮机;,hibernate,Hibernate,我想持久化以下类,包括“bar” Class Foo { Map<String, List<String>> bar; // ... // other stuff..... } 福班 { 地图栏; // ... //其他东西。。。。。 } 在Hibernate中如何执行此操作? 如果可能,如何在带注释的Hibernate中实现这一点 哦,映射键字符串的本质是它们的数量很少(5-40个),并且它们在Foo实例中是相同的。列表中的字符串在列表内和Foo实例之间都是
谢谢 字符串和列表都不是实体,您应该创建一个封装列表的包装类 别忘了塞特的
@Entity
public class Foo {
private MutableInt id = new MutableInt();
private Map<String, CustomList> customListMap = new HashSet<String, CustomList>();
@Id
@GeneratedValue
public Integer getId() {
return this.id.intValue();
}
public void setId(Integer id) {
return this.id.setValue(id);
}
@OneToMany
@MapKey(name="key")
public Map<String, CustomList> getCustomListMap() {
return customListMap;
}
// add convenience method
public void addBar(String key, String bar) {
if(customListMap.get(key) == null)
customListMap.put(key, new CustomList(new CustomListId(id, key)));
customListMap.get(key).getBar().add(bar);
}
}
@Entity
public class CustomList {
private CustomListId customListId;
private List<String> bar;
private String key;
@EmbeddedId
public CustomListId getCustomListId() {
return customListId;
}
@Column(insertable=false, updatable=false)
public String getKey() {
return this.key;
}
@CollectionOfElements
@JoinTable(name="BAR")
public List<String> getBar() {
return this.bar;
}
@Embeddable
public static class CustomListId implements Serializable {
private MutableInt fooId = new MutableInt();
private String key;
// required no-arg construtor
public CustomList() {}
public CustomList(MutableInt fooId, String key) {
this.fooId = fooId;
this.key = key;
}
public Integer getFooId() {
return fooId.intValue();
}
public void setFooId(Integer fooId) {
this.fooId.setValue(fooId);
}
// getter's and setter's
public boolean equals(Object o) {
if(!(o instanceof CustomListId))
return false;
CustomListId other = (CustomList) o;
return new EqualsBuilder()
.append(getFooId(), other.getFooId())
.append(getKey(), other.getKey())
.isEquals();
}
// implements hashCode
}
}
@实体
公开课Foo{
私有MutableInt id=new MutableInt();
私有映射customListMap=新HashSet();
@身份证
@生成值
公共整数getId(){
返回此.id.intValue();
}
公共无效集合id(整数id){
返回此.id.setValue(id);
}
@独身癖
@MapKey(name=“key”)
公共映射getCustomListMap(){
返回customListMap;
}
//添加方便方法
公共无效添加栏(字符串键、字符串栏){
if(customListMap.get(key)==null)
put(key,newcustomlist(newcustomlistid,id,key));
customListMap.get(key).getBar().add(bar);
}
}
和您的自定义客户列表(不要忘记setter的)
@实体
公共类自定义列表{
私有CustomListId CustomListId;
私人列表栏;
私钥;
@嵌入ID
公共CustomListId getCustomListId(){
返回customListId;
}
@列(可插入=false,可更新=false)
公共字符串getKey(){
返回此.key;
}
@收藏费
@可接合(name=“BAR”)
公共列表getBar(){
返回此.bar;
}
@可嵌入
公共静态类CustomListId实现了可序列化{
私有MutableInt fooId=new MutableInt();
私钥;
//不需要参数构造函数
公共CustomList(){}
公共CustomList(MutableInt fooId,字符串键){
this.fooId=fooId;
this.key=key;
}
公共整数getFooId(){
返回fooId.intValue();
}
公共void setFooId(整数fooId){
this.fooId.setValue(fooId);
}
//盖特和塞特
公共布尔等于(对象o){
如果(!(CustomListId的实例))
返回false;
CustomListId其他=(CustomList)o;
返回新的EqualBuilder()
.append(getFooId(),other.getFooId())
.append(getKey(),other.getKey())
.isEquals();
}
//实现哈希代码
}
}
您甚至可以创建一个名为getBar的自定义方法,该方法透明地封装customListMap,如下所示
@Entity
public class Foo {
...
public Map<String, List<String>> getBar() {
Map<String, List<String>> bar = new HashMap<String, List<String>>();
for(Entry<String, CustomList> e: customListMap())
bar.put(e.getKey(), e.getValue().getBar());
return bar;
}
}
@实体
公开课Foo{
...
公共地图getBar(){
映射栏=新的HashMap();
对于(条目e:customListMap())
put(e.getKey(),e.getValue().getBar());
返回杆;
}
}
数据库表示形式是什么?CustomList类在CustomList类中,给了我一个编译错误:CustomList.java:33:CustomList已经在未命名的包中定义了公共静态类CustomList implements Serializable{