2

spring.xmlで正常に機能する次のコードがあります...

<beans:bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>org.assessme.com.entity.User</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="dataSource" ref="dataSource" />
                <beans:property name="packagesToScan"
        value="org.assessme.com.entity.*" />
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </beans:prop>
                <beans:prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
                </beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
                <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

Beans:listからわかるように、最初にUserオブジェクトを機能させるだけです。

以下に示すように、すべてのエンティティは同じフォルダにあります

エンティティ

問題は、このリストに次のような別のクラスを追加すると...

org.assessme.com.entity.Campaign

Tomcatを起動すると、次の例外が発生します...

 Caused by: org.hibernate.AnnotationException: No identifier specified for entity: org.assessme.com.entity.Campaign
    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:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 132 more

必要に応じて、以下のエンティティコード...

Campaign.java

package org.assessme.com.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "campaigns")
public class Campaign {

    @Column(name = "name")
    private String name;

    @Column(name = "id")
    private String id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

User.java

package org.assessme.com.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "users")
public class User {

    @Column(name = "firstName")
    private String firstName;

    @Column(name = "lastName")
    private String lastName;

    @Column(name = "email")
    private String email;

    @Column(name = "password")
    private String password;

    @Column(name = "username")
    private String username;

    @Column(name = "access")
    private Integer access;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id")
    private long id;


    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Integer getAccess() {
        return access;
    }
    public void setAccess(Integer access) {
        this.access = access;
    }
}
4

1 に答える 1

7

Campaign問題は、注釈が付けられたプロパティがないことだと思います@Id。次のように修正します。

@Id
@Column(name = "id")
private String id;
于 2012-10-09T17:40:29.683 に答える