使用Hibernate持久保存接口 [英] Persist collection of interface using Hibernate

查看:131
本文介绍了使用Hibernate持久保存接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Entity 
@Table(name =zoo )
public class Zoo {
@OneToMany
private Set< Animal>动物=新的HashSet< Animal>();
}

//只是一个标记界面
public interface Animal {
}

@Entity
@Table(name =dog)
public class Dog implements Animal {
// ID和其他属性
}

@Entity
@Table(name = cat)
public class Cat implements Animal {
// ID和其他属性
}

当我尝试坚持动物园时,Hibernate抱怨道:

 使用@OneToMany或@ManyToMany定位一个未映射的类:blubb.Zoo.animals [blubb.Animal] 

我知道 targetEntity - @property <@ c $ c> @OneToMany 但这意味着,只有狗或猫可以住在我的动物园里。



有没有什么办法可以用Hibernate持久化一个接口的集合,它有几个实现?

解决方案

JPA注释在接口上不受支持。从 Java持久化与Hibernate (p.210):


请注意,JPA规范
doesn在界面上不支持任何映射注释
!这将在
规范的未来版本中解决
;当你阅读这本
的书时,可能会有
和Hibernate Annotations。

可能的解决方案是应该使用带有 TABLE_PER_CLASS 继承策略的抽象实体(因为您不能在关联中使用映射超类 - 它不是实体)。像这样:

  @Entity 
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractAnimal {
@Id @GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
...
}

@Entity
public class Lion extends AbstractAnimal implements Animal {
...
}

@Entity
public class Tiger extends AbstractAnimal implements Animal {
...
}

@实体
public class Zoo {
@Id @GeneratedValue
私人长ID;

@OneToMany(targetEntity = AbstractAnimal.class)
private Set< Animal>动物=新的HashSet< Animal>();

...
}

但没有太多在保持IMO接口方面的优势(实际上,我认为持久化类应该是具体的)。

参考文献




I want to persist my litte zoo with Hibernate:

@Entity
@Table(name = "zoo") 
public class Zoo {
    @OneToMany
    private Set<Animal> animals = new HashSet<Animal>();
}

// Just a marker interface
public interface Animal {
}

@Entity
@Table(name = "dog")
public class Dog implements Animal {
    // ID and other properties
}

@Entity
@Table(name = "cat")
public class Cat implements Animal {
    // ID and other properties
}

When I try to persist the zoo, Hibernate complains:

Use of @OneToMany or @ManyToMany targeting an unmapped class: blubb.Zoo.animals[blubb.Animal]

I know about the targetEntity-property of @OneToMany but that would mean, only Dogs OR Cats can live in my zoo.

Is there any way to persist a collection of an interface, which has several implementations, with Hibernate?

解决方案

JPA annotations are not supported on interfaces. From Java Persistence with Hibernate (p.210):

Note that the JPA specification doesn’t support any mapping annotation on an interface! This will be resolved in a future version of the specification; when you read this book, it will probably be possible with Hibernate Annotations.

A possible solution would be to use an abstract Entity with a TABLE_PER_CLASS inheritance strategy (because you can't use a mapped superclass - which is not an entity - in associations). Something like this:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractAnimal {
    @Id @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;
    ...
}

@Entity
public class Lion extends AbstractAnimal implements Animal {
    ...
}

@Entity
public class Tiger extends AbstractAnimal implements Animal {
    ...
}

@Entity
public class Zoo {
    @Id @GeneratedValue
    private Long id;

    @OneToMany(targetEntity = AbstractAnimal.class)
    private Set<Animal> animals = new HashSet<Animal>();

    ...
}

But there is not much advantages in keeping the interface IMO (and actually, I think persistent classes should be concrete).

References

这篇关于使用Hibernate持久保存接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆