1

Could you please any one explain what is the meaning of below declaration

@Column(nullable = false )
@XmlElement(required = true, nillable = true )

which means difference between nullable and nillable ?

Thirunavukkarasu
  • 361
  • 1
  • 2
  • 13
  • It would help if you would add the import statements for the annotations as well. –  Sep 25 '18 at 07:45
  • `XmlElement` is not part of JPA, but instead it comes from JAXB. So the two annotations and their `nullable` and `nillable` elements are unrelated. Having said this, https://stackoverflow.com/questions/5913200/why-is-it-called-nillable explains why `nillable` is used in the context of XML (which JAXB deals with). – M A Sep 25 '18 at 07:45

1 Answers1

5

The @Column annotation comes from JPA (the Java Persistence API) and specifies how a field is mapped to a database column. Using the nullable attribute of the annotation, you specify whether NULL values should be allowed in the database for this field.

The @XmlElement annotation comes from JAXB (the Java API for XML Binding) and specifies how the field should be treated when converting this object to and from XML. If you set the nillable attribute to true, it means that in the XML it's allowed for the corresponding element to have an xsi:nil="true" attribute.

Note that in XML there are subtle differences between an XML element being not present, being present but with an empty value and having an xsi:nil attribute.

Whether it's appropriate to use an @XmlElement annotation with nillable = true attribute depends on the requirements for your application.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • @Jasper, In my entity class we have declared @Column(nullable=false) in that circumstances null values are allowed to persists in db or not ? – Thirunavukkarasu Sep 25 '18 at 07:54
  • If you set `nullable = false` in the `@Column` annotation, then obviously `NULL` values in the database are not allowed. – Jesper Sep 25 '18 at 08:28