JPA映射接口 [英] JPA mapping interfaces

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

问题描述

当List类型是接口时,我无法创建映射。它看起来像我需要创建一个抽象类,并使用鉴别器列是这种情况?我宁愿不必为抽象类只包含一个抽象方法,我宁愿保持接口。

我有一个接口让它调用它帐户

p>

  public interface Account {
public void doStuff();





$ b现在我有两个具体的实现者Account
OverSeasAccount和OverDrawnAccount

  public class OverSeasAccount implements Account {
public void doStuff(){
// do overseas type stuff








$ b

  public class OverDrawnAccount implements Account {
public void doStuff(){
// do overDrawn type stuff
}
}

我有一个名为Work with a List的类

 私人列表<帐户>账户; 

我正在查看歧义字段,但我似乎只能对抽象类进行此操作。是这样吗?任何指针赞赏。我可以使用鉴别器来接口吗?

解决方案

我认为有可能使接口成为映射的超类型。您可能无法使用注释。注释在xml配置文件中可以很好地发挥作用,因此您可能必须使用所需的映射将hibernate配置文件添加到项目中。但是您可以保留项目其余部分的注释。



这个问题更多地讨论它。它似乎最终以一个建议,以及如何与注释做到这一点,谁知道。我建议xml现在还是比较安全的这个 page 的文档解释了所需的xml映射。


I am having trouble creating a mapping when the List type is an interface. It looks like I need to create an abstract class and use the discriminator column is this the case? I would rather not have to as the abstract class will just contain an abstract method and I would rather just keep the interface.

I have an interface lets call it Account

public interface Account {
 public void doStuff();
}

Now I have two concrete implementors of Account OverSeasAccount and OverDrawnAccount

public class OverSeasAccount implements Account {
 public void doStuff() {
   //do overseas type stuff
 }
}

AND

public class OverDrawnAccount implements Account {
 public void doStuff() {
   //do overDrawn type stuff
 }
}

I have a class called Work with a List

private List<Account> accounts; 

I am looking at discriminator fields but I seem to be only able do this for abstract classes. Is this the case? Any pointers appreciated. Can I use discriminators for interfaces?

解决方案

I think that it is possible to to make an interface the supertype of a mapping. You may not be able to use annotations though. Annotations play well with xml config files so you might have to add a hibernate config file to your project with the mappings that you need. But you will be able to keep the annotations for the rest of your project.

This issue discusses it more. It seems to end with a suggestion as to how to do it with annotations so who knows. I would suggest that xml is still safer for now This page of the docs explains the xml mapping needed.

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

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