我想使用表格格式
表“帐户”
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
我不知道为什么需要在此表上使用主键。
你是如何解决这个问题的?
答案 0 :(得分:1)
在该表上创建一个PRIMARY KEY。
Hibernate需要一个PRIMARY KEY。期。感叹号。
来自Hibernate文档:
&#34;映射类必须声明数据库表的主键列。&#34;
参考:https://docs.jboss.org/hibernate/core/4.3/manual/en-US/html/ch05.html#mapping-declaration-id
我们可以继续讨论为什么Hibernate需要声明主键,我们可以继续讨论你想要的东西。但在讨论结束时,最终归结为:Hibernate需要该表上的主键。