Hibernate 休眠:@UniqueConstraint和@manytone字段?
使用以下代码:Hibernate 休眠:@UniqueConstraint和@manytone字段?,hibernate,groovy,Hibernate,Groovy,使用以下代码: @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 Str
@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
}
我得到以下错误:
org.hibernate.AnnotationException: Unable to create unique key constraint (account, name) on table Friend: account not found
这似乎与@ManyToOne约束有关,我想它实际上创建了一个单独的UniqueConstraint
无论如何,如果我把它拿出来,没有人会抱怨UniqueConstraint,但是还有一个错误让我相信它必须留在里面
org.hibernate.MappingException: Could not determine type for: com.mksoft.fbautomate.domain.Account, at table: Friend, for columns: [org.hibernate.mapping.Column(account)]
有没有提示我如何创建这样一个所需的约束(即,帐户和名称的每个组合只出现一次??)
谢谢大家!
米沙解决方案:
@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
}
说明:
生成表中@ManyToOne的列名可能类似于account\u id,而不是account。@UniqueConstraint要求的是确切的列名,而不是您在类中使用的名称。为了确保它正常工作,请使用@JoinColumn为该列指定一个特定的名称,并确保在UniqueConstraint中使用相同的名称。我使用@JoinColumn,这没问题,但我运行的是liquibase脚本“diffAll”,这个创建的.yaml文件包括mysql和oracle mysql中的列名为“profile”,oracle数据库中的列名为“profile\u id” 当我写入“@JoinColumn(name=“profile”)时,请尝试删除“profile\u id”列,并为oracle db添加“profile”列 我是如何为多个数据库创建@UniqConstraint的 多谢各位。
问候。彼得的解决方案对你有用吗?