i try to get the object of User. and then the brands and merchants will not be separate with filed 'type'.
the error like this:
org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: com.mofalife.hibernate.entity.Merchant (loaded object was of wrong class class com.mofalife.hibernate.entity.Brand)
table of 'base'
Code:
type id name user_fk
0 1 brand 1 1
0 2 brand 2 1
1 3 merchant 1 1
1 4 merchant 2 1
1 5 merchant 3 1
Code:
package com.mofalife.hibernate.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
@Entity
@Table(name = "base")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
public abstract class Base implements Serializable
{
private static final long serialVersionUID = 7470345766061076963L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Integer id;
@Column(name = "name", columnDefinition = "varchar(20)", length = 20)
protected String name;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
Code:
package com.mofalife.hibernate.entity;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("0")
public class Brand extends Base
{
private static final long serialVersionUID = -4085763045748639836L;
}
Code:
package com.mofalife.hibernate.entity;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("1")
public class Merchant extends Base
{
private static final long serialVersionUID = -1484033368245170848L;
}
Code:
package com.mofalife.hibernate.entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "user")
public class User implements Serializable
{
private static final long serialVersionUID = 1803143298290719754L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@JoinColumn(name = "user_fk")
private Set<Brand> brands = new HashSet<Brand>();
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@JoinColumn(name = "user_fk")
private Set<Merchant> merchants = new HashSet<Merchant>();
public void addBrand(Brand brand)
{
brands.add(brand);
}
public void addMerchant(Merchant merchant)
{
merchants.add(merchant);
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public Set<Brand> getBrands()
{
return brands;
}
public void setBrands(Set<Brand> brands)
{
this.brands = brands;
}
public Set<Merchant> getMerchants()
{
return merchants;
}
public void setMerchants(Set<Merchant> merchants)
{
this.merchants = merchants;
}
}