Reputation: 4418
I have created one table which contains one decimal field PERCENTAGE
and set default value as 0.0 but while inserting data from hibernate it not inserting default value inserting null instead of 0.0.
Create Table Query ::
CREATE TABLE table (
ID INT(10) NOT NULL AUTO_INCREMENT,
DOB DATE NOT NULL,
RELATIONSHIP VARCHAR(50) NOT NULL,
PERCENTAGE DECIMAL(10,2) DEFAULT 0.0,
PRIMARY KEY (ID)
);
In Domain Object
@Entity
@Table(name = "table")
public class Table implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = IDENTITY)
private Integer ID;
@Temporal(TemporalType.DATE)
private Date dob;
private BigDecimal percentage;
private String relationship;
Save data into DB
MapInsuranceNominee mapInsuranceNominee = new MapInsuranceNominee();
mapInsuranceNominee.setDob(vo.getDob()));
mapInsuranceNominee.setRelationship(vo.getRelation());
Please suggest what is wrong in the code.
Upvotes: 1
Views: 1776
Reputation: 1
Alter the percentage column and add "not null" in addition to default value.
Upvotes: 0
Reputation: 2592
You can try @Column(columnDefinition="Decimal(10,2) default '0.00'")
Try this
Upvotes: 0
Reputation: 692181
What's wrong is that the default value should be set in the code.
When saving an entity, you tell Hibernate to save the entity, with all its fields, including null ones. So it inserts null.
Default values set in the database are only used when you don't insert anything in the column (i.e when the column doesn't even appear in the insert query). That won't happen with Hibernate.
Upvotes: 2