如何在Java-EE中使用hibernate实现ManyToMany关系?

问题描述 投票:0回答:1

在构建具有依赖项的maven项目时,我不断收到此错误:

Exception Description: The target entity of the relationship attribute 
[template] on the class [class pt.ipleiria.dae.entities.Configuration] 
cannot be determined.  When not using generics, ensure the target entity is 
defined on the relationship mapping.

我有这两个实体,代码如下:

@ManyToMany(mappedBy="configurations")
private Template template;
private String name;
private ConfigurationState state;
private String version;
private String description;
private List<Module> modules;
private List<Resource> resources;
private List<String> parameters;
private List<String> extensions;
private String contrato;

模板(关系的所有者):

@ManyToMany
@JoinTable(name="TEMPLATE_CONFIGURATIONS",
joinColumns=
    @JoinColumn(name="ID", referencedColumnName="ID"),
inverseJoinColumns=
    @JoinColumn(name="ID", referencedColumnName="ID")
)
private List<Configuration> configurations;

我希望有多对多的关系,因为“模板”包含几个“配置”,而“配置”可以在几个“模板”(配置)中。

hibernate maven jpa java-ee glassfish-5
1个回答
2
投票

一般来说,你定义的例外是在定义Generics时定义Many,如here所解释的那样。

虽然在你的情况下,还有一些其他问题。

由于您已在@ManyToManyConfiguration之间应用了Template关系,因此应在Configuration Entity中对此进行定义。

@ManyToMany(mappedBy="configurations")
 private List<Template> templates;

如果您要求配置只能在模板上有模板可以有多个配置,那么您应该使用OneToMany关系。在配置实体中,您将拥有:

@ManyToOne(mappedBy="configurations")
private Template template;

在模板实体中,您将拥有

@OneToMany
private List<Configuration> configurations;

希望这可以帮助!!

© www.soinside.com 2019 - 2024. All rights reserved.