如何使用Hibernate注释在连接表上创建索引? [英] How do I create an index on join tables using Hibernate annotations?

查看:141
本文介绍了如何使用Hibernate注释在连接表上创建索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class Job {

...

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name =jobs_resource_locations)
@ForeignKey(name =job_inputs_fk )
@Index(name =job_inputs_fk)
private List< FileSystemLocation>输入;

这样的事情在ManyToOne上可以很好地工作,如下所示:

  @ManyToOne 
@JoinColumn(name =service_call_id,referencedColumnName =id)
@ForeignKey(name =job_service_call_fk)
@Index(name =job_service_call_fk)
private ServiceCall serviceCall;

我想确保外键在PostgreSQL上获得索引,并且模式在MySQL上看起来类似,因此具有相同名称的@ForeignKey和@Index(MySQL始终创建一个与FK同名的索引)。

我无法在反面创建索引因为FileSystemLocation不知道这种关系。因此,Hibernate在Job中找不到任何列来索引:

$ b

前面的例子失败了。 > org.hibernate.MappingException:无法从表格作业中的物理名称null找到逻辑列名称

有谁知道如何在使用Hibernate的JoinTable外键上创建索引?解析方案

这不完全是你的答案想要收到,但这是预期的行为。换句话说:这不被支持。有关更多详情,请参阅以下JIRA:

https://hibernate.atlassian.net/browse/HHH-4263


I have a relationship as follows using Hibernate annotations, this is what I tried:

public class Job{

...

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "jobs_resource_locations")
@ForeignKey(name = "job_inputs_fk")
@Index(name="job_inputs_fk")
private List<FileSystemLocation> inputs;

This sort of thing works nicely on ManyToOne like so:

@ManyToOne
@JoinColumn(name = "service_call_id", referencedColumnName = "id")
@ForeignKey(name = "job_service_call_fk")
@Index(name = "job_service_call_fk")
private ServiceCall serviceCall;

I wanted to ensure that the foreign key gets indexed on PostgreSQL and that the schema looks similar on MySQL, hence the @ForeignKey and @Index with the same name (MySQL always creates an index with the same name as the FK).

I cannot create the index on the inverse side because FileSystemLocation is unaware of the relationship. Hence the JoinTable.

The former example fails since Hibernate finds no column in Job to index:

org.hibernate.MappingException: Unable to find logical column name from physical name null in table jobs

Does anyone know how to create indices on JoinTable foreign keys using Hibernate?

解决方案

It's not exactly the answer you would like to receive, but this is the expected behavior. In other words: this is not supported. See the following JIRA for more details:

https://hibernate.atlassian.net/browse/HHH-4263

这篇关于如何使用Hibernate注释在连接表上创建索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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