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

用户.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 回答