Java Spring Hibernate继承错误的类Instanceate
我正在使用Spring4和Hibernate 我在hibernate和spring中有一个继承结构: 泛型类“ResourceState”和子类“VideoState”和“AudioState” 我的问题是Hibernate生成'ResourceState'实例,而不是'VideoState'或'AudioState'实例 SQL代码:Java Spring Hibernate继承错误的类Instanceate,java,spring,hibernate,inheritance,Java,Spring,Hibernate,Inheritance,我正在使用Spring4和Hibernate 我在hibernate和spring中有一个继承结构: 泛型类“ResourceState”和子类“VideoState”和“AudioState” 我的问题是Hibernate生成'ResourceState'实例,而不是'VideoState'或'AudioState'实例 SQL代码: CREATE TABLE `RESOURCE_STATE` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREME
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;
}
}
和休眠配置:
<?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>
org.hibernate.dialogue.hsql方言
真的
创造
我真的不明白为什么hibernate不能生成正确的类实例
谢谢您的帮助。您所说的“Hibernate生成实例”是什么意思?您能展示一下从数据库中获取实例的代码吗?我有一个用户类和资源类,用户和资源有多对多关系,映射到用户\u HAS\u资源表和java UserHasResource类中。UserHasResource类具有ResourceState参数。完整的类树是自动生成的,因此您需要有三个单独的表,这可以通过
单表
策略和@DiscriminatorColumn
实现。