我如何让Hibernate在外键(JoinColumn)上创建一个索引? [英] How can I ask Hibernate to create an index on a foreign key (JoinColumn)?

查看:294
本文介绍了我如何让Hibernate在外键(JoinColumn)上创建一个索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



class User

  @CollectionOfElements 
@JoinTable(name =user_type,joinColumns = @JoinColumn(name =user_id))
@Column(name =type,nullable = false)
private List< ;字符串> types = new ArrayList< String>();

}

是一个名为user_type的表,它有两列,一个是user_id,另一个是type。

当我使用hbm2ddl生成ddls时,我可以将这个表与user_id上的外键约束一起使用。但是,这个列没有这个索引。我需要这个索引。我如何让休眠状态为我生成这个索引?

谢谢!

解决方案

试试@Index注释。

  @Index(name =user_type_index)

还有一个与连接表一起使用的@IndexColumn注解,但它似乎并没有真正创建索引,而是控制了哪个字段定义了列表语义的顺序。



此上下文中的@Index列似乎在连接表上创建索引。


This is my model.

class User{

   @CollectionOfElements 
    @JoinTable(name = "user_type", joinColumns = @JoinColumn(name = "user_id"))
    @Column(name = "type", nullable = false)
  private List<String> types = new ArrayList<String>();

}

You can imagin there would be a table called "user_type", which has two columns, one is "user_id", the other is "type".

And when I use hbm2ddl to generate the ddls, I can have this table, along with the foreign key constraint on "user_id". However, there is no index of this for this column. And I need this index. How can I let hibernate to generate this index for me?

Thank you!

解决方案

Try an @Index annotation.

@Index(name="user_type_index")

There is also an @IndexColumn annotation used with join tables, but it doesn't seem to actually create an index, but controls which field defines order in list semantics.

The @Index column in this context does seem to create an index on the join table.

这篇关于我如何让Hibernate在外键(JoinColumn)上创建一个索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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