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();
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 {
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With