简体   繁体   中英

Hibernate One-To-Many, i want not use primary Key to Many Entity

I want use table format

Table "Account"

CREATE TABLE `account` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`id`)
)

Table "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`)

)

This is why I want to use account optional variable.

This Optional variable is not need Primary Key.

Then source by

@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;

}

but this source is crash;

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

I do not know why need to use Primary Key on this Table.

How do you solve this?

Create a PRIMARY KEY on that table.

Hibernate requires a PRIMARY KEY. Period. Exclamation Point.

From the Hibernate documentation:

"Mapped classes must declare the primary key column of the database table."

Ref: https://docs.jboss.org/hibernate/core/4.3/manual/en-US/html/ch05.html#mapping-declaration-id


We could go on about why Hibernate needs a primary key declared, and we could go on about what you want. But at the end of that discussion, in the end, it boils down to simply this: Hibernate requires a primary key on that table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM