我想用表格式Hibernate的一對許多,我想不使用主鍵,把許多實體
表 「帳戶」
CREATE TABLE `account` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
)
表 「Account_options」
CREATE TABLE `account_options` (
account_id bigint(20) NOT NULL,
name varchar(50) NOT NULL,
value varchar(255) NOT NULL,
KEY `A` (`account_id`)
CONSTRAINT `A` FOREIGN KEY (`account_id`) REFERENCES `account` (`id`)
)
這就是爲什麼我想使用帳戶可選變量。
此可選變量不需要主鍵。
然後通過
@Entity
@Table (name = "account")
public class Account {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private long id;
@OneToMany(cascade = { CascadeType.ALL })
@JoinColumn(name = "account_id")
private List<AccountOption> options = new ArrayList<AccountOption>();
}
@Entity
@Table (name = "account_options")
public class AccountOption {
@ManyToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "account_id")
private Account account;
@Column (length = 50)
private String name;
@Column (length = 255)
private String value;
}
源,但這個源的碰撞;
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: test.domain.AccountOption
at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:272)
at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:227)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:712)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:636)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:359)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1206)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:717)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 34 more
我不知道爲什麼需要在此表上使用主鍵。
你如何解決這個問題?
解決方案:@ElementCollection – 2014-08-30 03:11:55
沒有主鍵的'account_options'聽起來像是一個錯誤的設計。如果您無法唯一識別一行,您將如何刪除某個帳戶的單個選項? – 2014-08-30 05:58:51