Spring Hibernate继承错误的类instanciate

时间:2015-05-31 14:44:41

标签: java spring hibernate inheritance

我正在使用Spring 4和Hibernate 我在hibernate和spring中有一个继承结构: 通用类'ResourceState'和SubClass'VideoState'和'AudioState'

我的问题是Hibernate生成'ResourceState'的实例而不是'VideoState'或'AudioState'实例......

SQL代码:

CREATE TABLE `RESOURCE_STATE` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

CREATE TABLE `VIDEO_STATE` (
  `resource_state_id` bigint(20) unsigned NOT NULL,
  `reproduction_point` int(10) unsigned NOT NULL DEFAULT '0',
  `volume` decimal(10,5) unsigned NOT NULL DEFAULT '100.00000',
  `state` int(2) NOT NULL DEFAULT '-1',
  PRIMARY KEY (`resource_state_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `AUDIO_STATE` (
  `resource_state_id` bigint(20) unsigned NOT NULL,
  `reproduction_point` int(10) unsigned NOT NULL DEFAULT '0',
  `volume` decimal(10,5) unsigned NOT NULL DEFAULT '100.00000',
  `state` int(2) NOT NULL DEFAULT '-1',
  PRIMARY KEY (`resource_state_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Java代码:

@Entity
@Table(name = "RESOURCE_STATE")
@Inheritance(strategy=InheritanceType.JOINED)
public class ResourceState {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;

    public Long getId() {
        return id;
    }

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

@Entity
@Table(name="AUDIO_STATE")
@PrimaryKeyJoinColumn(name="resource_state_id")
public class AudioState extends ResourceState{

    @Column(name="reproduction_point")
    private Integer reproductionPoint;
    @Column(name="volume")
    private Double volume;
    @Column(name="state")
    private Integer state;

    public Integer getReproductionPoint() {
        return reproductionPoint;
    }
    public void setReproductionPoint(Integer reproductionPoint) {
        this.reproductionPoint = reproductionPoint;
    }
    public Double getVolume() {
        return volume;
    }
    public void setVolume(Double volume) {
        this.volume = volume;
    }
    public Integer getState() {
        return state;
    }
    public void setState(Integer state) {
        this.state = state;
    }

@Entity
@Table(name="VIDEO_STATE")
@PrimaryKeyJoinColumn(name="resource_state_id")
public class VideoState extends ResourceState{

    @Column(name="reproduction_point")
    private Integer reproductionPoint;
    @Column(name="volume")
    private Double volume;
    @Column(name="state")
    private Integer state;

    public Integer getReproductionPoint() {
        return reproductionPoint;
    }
    public void setReproductionPoint(Integer reproductionPoint) {
        this.reproductionPoint = reproductionPoint;
    }
    public Double getVolume() {
        return volume;
    }
    public void setVolume(Double volume) {
        this.volume = volume;
    }
    public Integer getState() {
        return state;
    }
    public void setState(Integer state) {
        this.state = state;
    }

@Entity
@Table(name="USER_HAS_RESOURCE")
public class UserHasResource {

    @Id
    @GeneratedValue
    @Column(name="id")
    private Long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="resource_id")
    private Resource resource;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="resource_state_id")
    private ResourceState resourceState;

    public UserHasResource() {}

    public Long getId() {
        return id;
    }

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

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Resource getResource() {
        return resource;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }

    public ResourceState getResourceState() {
        return resourceState;
    }

    public void setResourceState(ResourceState resourceState) {
        this.resourceState = resourceState;
    }   
}

和Hibernate Config:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>        
  <session-factory>
    <property name="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>  
    <mapping class="it.unica.model.User" />
    <mapping class="it.unica.model.Video"/>
    <mapping class="it.unica.model.Audio"/>
    <mapping class="it.unica.model.Resource"/>
    <mapping class="it.unica.model.ResourceState"/>
    <mapping class="it.unica.model.VideoState"/>
    <mapping class="it.unica.model.AudioState"/>
    <mapping class="it.unica.model.Device"/>
    <mapping class="it.unica.model.UserInDevice"/>
    <mapping class="it.unica.model.UserHasResource"/>
  </session-factory>
</hibernate-configuration>

我真的不明白为什么hibernate不能创建正确的类实例。

感谢您的帮助。

0 个答案:

没有答案