2

I set "firm_name" default value in Ben using annotation. But when I insert data it will add NULL in database. I want to set default value into database so that I just set the values which are require.
Other column values set as default value which is set into Bean. Following is my Code.But it is not working. I will inset nNULL value into database.

@Entity
@Table(name = "my_leads")
public class My_leads{

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id")
    int id;

    @Column(name = "name", length = 100,columnDefinition = "varchar(255) default 'NA'")
    String name;

    @Column(name = "enrtyDate", insertable = false, updatable = false, nullable = false,columnDefinition = "datetime default NOW()")
    Date enrtyDate;

    @Column(name = "mobileNo", nullable = false, length = 100)
    String mobileNo;

    @Column(name = "firm_name", length = 100, nullable = false,columnDefinition = "varchar(255) default 'No Refrence'")
    String firm_name;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getEnrtyDate() {
        return enrtyDate;
    }

    public void setEnrtyDate(Date enrtyDate) {
        this.enrtyDate = enrtyDate;
    }

    public String getMobileNo() {
        return mobileNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }


    public String getFirm_name() {
        return firm_name;
    }

    public void setFirm_name(String firm_name) {
        this.firm_name = firm_name;
    }

}


My_Leads lead=new My_Leads();
lead.setUser(1);
lead.setMobileNo("1234567896");
lead.setName("Sajan");
lead.setPriority(1);
lead.setStage(1);
lead.setCampain(1);
adminService.SaveLead(lead)
Milad Rashidi
  • 1,296
  • 4
  • 22
  • 40
Rahul
  • 131
  • 3
  • 10

2 Answers2

4

The code you wrote is not a way to set a default value by Hibernate - it's actually for a database.

When you create a table in the database you can define a column such way that if you try inserting a null value the default value will be inserted instead. That's what you did here varchar(255) default 'No Refrence'.

If your table is already created, Hibernate is gonna ignore that statement. That statement is used only when Hibernate is creating the schema for you, using @Entity classes. If you go into your database and check column definitions you will see that your column has no default value, since it was not created by Hibernate.

You can delete your schema and let Hibernate create it for you, then the default value will work. Or you can edit your schema manually, adding default value to already existing column. For example:

ALTER TABLE my_leads ALTER COLUMN firm_name SET DEFAULT 'No Refrence'

If you let Hibernate generate your schema and still have this error - make sure those are actually null values, not NULL strings or something.

If you don't want to have the default values inserted by database, but by Hibernate, do this in your @Entity class:

String firm_name = "No Refrence";

Shadov
  • 5,421
  • 2
  • 19
  • 38
2

Whenever an entity is to be inserted, Hibernate is going to generate the following DML:

INSERT 
  INTO my_leads (id, name, enrtyDate, mobilNo, firm_name)
VALUES (?, ?, ?, ?, ?)

In your java code, you're simply not setting a value for firm_name so that property is null.

Whether or not the default value for the column is used is going to depend upon what database platform you're using and how it interprets NULL in this case.

For example, MySQL will see that Hibernate bound NULL for the firm_name and will therefore apply the default value for you rather than setting the column as NULL.

For example, SQL Server will see that the INSERT statement contains the field firm_name in the columns section and therefore will ignore the default value and use whatever value is supplied in the values section, thus the column will be set to NULL. The only way SQL Server will use the default value is if the column is omitted from the columns section of the insert statement.

The only way to guarantee that the default value is set regardless of your database platform is to make sure that your entity state adheres to that rule too. This means you either need to initialize firm_name with the default value in the field definition, in the constructor of your class or your business logic that constructs your entities.

Naros
  • 19,928
  • 3
  • 41
  • 71