Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Single Table Inheritance

Tags:

java

hibernate

I have two Entities BillingAddress and ShippingAddress mapped to TABLE ADDRESS having discriminator TYPE column.

@Entity
@Table(name = "address")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE",discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue(value = "1")
@NamedQueries({
    @NamedQuery(name = "Shippingaddress.findAll", query = "SELECT s FROM Shippingaddress s")})
public class Shippingaddress  implements Serializable{}

@Entity
@Table(name = "address")
@DiscriminatorValue(value = "2")
@NamedQueries({
    @NamedQuery(name = "Billingaddress.findAll", query = "SELECT b FROM Billingaddress b")})
public class Billingaddress extends Shippingaddress implements Serializable {}

I am able to save/update the two address type successfully in DB with different TYPE values.

The problem I am facing is when I am querying ADDRESS table as follows:

Session session=getCurrentSession();
        Query query=session.createQuery("from Billingaddress where userId.userId=:userId");
        query.setLong("userId", userId);
        billingaddress=(Billingaddress)query.uniqueResult();

This works fine , but querying ShippingAddress throws NonUniqueResult Exception ie I get both Shippingaddress and Billingaddress in result. Please let me know , what should I do differently.

Session session=getCurrentSession();
        Query query=session.createQuery("from Shippingaddress where userId.userId=:userId");
        query.setLong("userId", userId);
        shippingaddress=(Shippingaddress)query.uniqueResult();
like image 948
Amardeep Avatar asked Oct 20 '22 03:10

Amardeep


2 Answers

DiscriminatorColumn & DiscriminatorValue don't works on the same class... i suggest you to define an abstract class like this...

Try this configuration for Hierarchy structure

ShippingaddressAbstract.java

@Table(name = "address")
@Inheritance
@DiscriminatorColumn(name = "TYPE",discriminatorType = DiscriminatorType.INTEGER)
public abstract class ShippingaddressAbstract implements java.io.Serializable {

}

Shippingaddress.java

@Entity
@DiscriminatorValue(value = "1")
public class Shippingaddress extends ShippingaddressAbstract {

}

Billingaddress.java

@Entity
@DiscriminatorValue(value = "2")
public class Billingaddress  extends ShippingaddressAbstract {

}
like image 20
Xstian Avatar answered Oct 22 '22 17:10

Xstian


another solution is to create an abstract class Address and map the column in the parent class with "insertable = false, updatable = false"

Then you can add the type in your query:

@Entity
@Table(name = "address")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE",discriminatorType = DiscriminatorType.INTEGER)
@NamedQueries({
@NamedQuery(name = "Address.findAll", query = "SELECT s FROM Address s where type = :type")})
public class Address implements Serializable {
    @Column(name="TYPE", insertable=false, updatable=false)
    private Integer type;
}

@Entity
@DiscriminatorValue(value = "1")
@NamedQueries({
@NamedQuery(name = "Shippingaddress.findAll", query = "SELECT s FROM Shippingaddress s")})
public class Shippingaddress  implements Serializable{}

@Entity
@DiscriminatorValue(value = "2")
@NamedQueries({
@NamedQuery(name = "Billingaddress.findAll", query = "SELECT b FROM Billingaddress b")})
public class Billingaddress extends Shippingaddress implements Serializable {}

then you can write "from Address where type = 1" is the same as "from Shippingaddress" and "from Address" will retrieve all rows

like image 125
JavaVini Avatar answered Oct 22 '22 17:10

JavaVini