Quantcast
Channel: JPA Embeddeble PK and nullable field - Stack Overflow
Viewing all articles
Browse latest Browse all 5

JPA Embeddeble PK and nullable field

$
0
0

I have a table called "Attributes" which has a PK of 3 fields which can be null. In this case Style_no is not null but item_no and size_no are null.

Is it possible to have a Embeddeble PK where fields can be null?

@Entity@Table(name="ATTRIBUTE")public class Attribute {    @EmbeddedId    private AttributePK attrPK;    ...    @Embeddable    public static class AttributePK implements Serializable{        private static final long serialVersionUID = -2976341677484364274L;        @Column(name="STYLE_NO", nullable=true)        protected String styleNo;        @Column(name="ITEM_NO", nullable=true)        protected String itemNo;        @Column(name="SIZE_NO", nullable=true)        protected String sizeNo;    ...

When i try to reference over one field e.g. style_no the result amount is 0.

@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, orphanRemoval=true, mappedBy="attrPK.styleNo")@MapKey(name="attrPK.name")public Map<String,Attribute> attributesX;

OR

@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, orphanRemoval=true)@JoinColumn(name="STYLE_NO", referencedColumnName="STYLE_NO")private List<Attribute> attributes;

When i remove item_no and size_no as pk im receiving a valid result.

Edit: To make my question more specific. Is per JPA guideline or "common sense" not allowed to use nullable fields for EmbeddebedId? If not, what annotions or logic do i need to add to make it work without adding another PK?Once filling the nullable field in the PK with values. The result is corrct.

Thank you very much!


Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>