休眠:@UniqueConstraint与@ManyToOne领域? [英] Hibernate: @UniqueConstraint with @ManyToOne field?

查看:109
本文介绍了休眠:@UniqueConstraint与@ManyToOne领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码:

  @Entity 
@Table(uniqueConstraints = [@ UniqueConstraint(columnNames = [ account,name])])
class Friend {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long id
@ManyToOne
公共账户账户
public String href
public String name
}

我得到以下错误:

pre $ org.hibernate.AnnotationException:无法在表上创建唯一的键约束(帐户,名称)朋友:找不到帐户

看起来这与@ManyToOne约束有关,我认为它实际上是创建一个单独的UniqueConstraint ???

在任何情况下,如果我拿出来,没有关于UniqueConstraint的抱怨,但是还有另一个错误让我相信它必须留在。

  org.hibernate.MappingException:无法确定com.mksoft.fbautoma的类型te.domain.Account,at table:Friend,for columns:[org.hibernate.mapping.Column(account)] 

任何提示我如何创建这样一个所需的约束(即,每个帐户和名称的组合只出现一次???)

谢谢!

解决方案

解决方案:
$ b

  @Entity 
@Table(uniqueConstraints = [@ UniqueConstraint(columnNames = [myaccountcolum,name])]
class Friend {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long id
@ManyToOne
@JoinColumn(name =myaccountcolum)
public帐户帐户
public String href
public String name
}

说明:
生成表中@ManyToOne的列名称可能与account_id而不是帐户类似。 @UniqueConstraint需要确切的列名称,而不是您在类中使用的名称。为了确保它正常工作,请使用@JoinColumn为此列指定一个特定名称,并确保您的UniqueConstraint中使用了相同的名称。


Using the following code:

@Entity 
@Table(uniqueConstraints=[@UniqueConstraint(columnNames=["account","name"])])
class Friend {
  @Id @GeneratedValue(strategy=GenerationType.AUTO)
  public Long id
  @ManyToOne
  public Account account
  public String href
  public String name
}

I get the following error:

org.hibernate.AnnotationException: Unable to create unique key constraint (account, name) on table Friend: account not found

It seems this has to do with the @ManyToOne constraint, which I imagine actually creates a separate UniqueConstraint???

In any case, if I take this out, there is no complaint about the UniqueConstraint, but there is another error which makes me believe it must be left in.

org.hibernate.MappingException: Could not determine type for: com.mksoft.fbautomate.domain.Account, at table: Friend, for columns: [org.hibernate.mapping.Column(account)]

Any hints how I can create such a desired constraint (i.e., that each combination of account and name occurs only once???)

Thank you!

Misha

解决方案

The solution:

@Entity 
@Table(uniqueConstraints=[@UniqueConstraint(columnNames=["myaccountcolum","name"])])
class Friend {
  @Id @GeneratedValue(strategy=GenerationType.AUTO)
  public Long id
  @ManyToOne
  @JoinColumn(name="myaccountcolum")
  public Account account
  public String href
  public String name
}

Explanation: The column name for the @ManyToOne in the generated table will probably be something like account_id instead of account. The @UniqueConstraint expects the exact column name, not the name you use in your class. To make sure it just works, give this column a specific name with @JoinColumn and make sure the same name is used in your UniqueConstraint.

这篇关于休眠:@UniqueConstraint与@ManyToOne领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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