How to set default boolean value in JPA - java

I have an attribute
private boolean include;
I would like to set its default value to true, so that in the database it must display True from default. Is this possible in JPA?

As far as i known there is no JPA native solution to provide default values.
Here it comes my workaround:
Non database portable solution
#Column(columnDefinition="tinyint(1) default 1")
private boolean include;
Java oriented solution
private boolean include = true;
Java oriented plus Builder pattern
#Column(nullable = false)
private Boolean include;
...
public static class Builder {
private Boolean include = true; // Here it comes your default value
public Builder include (Boolean include ) {
this.include = include ;
return this;
}
// Use the pattern builder whenever you need to persist a new entity.
public MyEntity build() {
MyEntity myEntity = new MyEntity ();
myEntity .setinclude (include );
return myEntity;
}
...
}
This is my favorite and less intrusive. Basically it delegates the task to define the default value to the Builder pattern in your entity.

Using JPA 2.1 and Oracle 11 this works for me by using Oracle type NUMBER of size 1:
Java:
#Column(name = "ENABLED", nullable = false)
private boolean enabled = true;
Create SQL script:
CREATE TABLE "ACCOUNT"(
"ID" NUMBER(10,0) NOT NULL ENABLE,
"NAME" VARCHAR2(255 CHAR) NOT NULL ENABLE,
"PASSWORD" VARCHAR2(255) NOT NULL ENABLE,
"ENABLED" NUMBER(1,0) DEFAULT 1 NOT NULL ENABLE,
PRIMARY KEY ("ID")
);

For PostgreSQL you can use boolean in definition
#Column(name = "isDeleted", columnDefinition = "boolean default true")
private boolean isDeleted = true;

Maybe will be useful for people who work with Microsoft SQL SERVER
#Column(columnDefinition="bit default 0")
private Boolean active;
Possible values: 0 or 1

In my case, for spring boot jpa, having the below syntax in entity class worked.
#Builder.Default
private boolean columnName = false;
or
#NotNull
#Builder.Default
#ColumnDefault("true")
private Boolean columnName = true;
#Builder.Default is to ensure we are having default values while constructing the object for this model, needed only when the entity model class is annotated with #Builder. If otherwise, even without having #Builder.Default, things are working fine as expected.

I've found that adding in the constructor is a good workaround to default new entities to a value:
public EntityName(){
this.fieldToDefault = default;
}

You can always use annotations #PreUpdate or #PrePersist on method where you will setup what should be done before update or before save into DB.
Or just simply setup the value private boolean include = true;

If you have defined default values in your database, you can choose the column annotation, and as parameter you use insertable = false, in this way when inserting a value it will choose the one that you marked by default in the database. Example:
In MySQL I have a person table with a status attribute of boolean type and it has by default the value true.
In your java class it would look like this:
//....
public class Person implements Serializable {
//.....
#Column(insertable = false)
private Boolean status;
//...
}
You can have more information about the column annotation HERE, it is well explained and it helped me a lot.

The easy way to set a default column value is to set it directly as an entity property value:
#Entity
public class Student {
#Id
private Long id;
private String name = "Ousama";
private Integer age = 30;
private Boolean happy = false;
}

If you are using MYSQL, I can save your 5-6 hours which you will waste if searching for the answer. I tried multiple ways but for me updating the maven dependency version of spring boot data jpa in pom.xml file
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.7.3</version>
</dependency>

private boolean include = true;

Related

hibernate correct way to set default value on database side

I'm new to hibernate, and I'm looking for the correct approach to set a default value on database side.
I hope the DDL generated by hibernate is equivalent to this:
create table SpaceGroup (
name varchar(255) not null,
isProtected boolean default FALSE not null,
primary key (name)
)
Here is my entity declaration:
#Entity
#DynamicInsert
public class SpaceGroup {
#Id
private String name;
#ColumnDefault("FALSE")
#Column(nullable = false)
private Boolean isProtected;
}
the problem with this approach is that hibernate didn't realize there is a db default value, so when I insert object it complains I do not give a value for a not null field:
org.hibernate.PropertyValueException: not-null property references a null or transient value : com.ziqi.models.SpaceGroup.isProtected
even adding a #DynamicInsert can not solve this issue
why hibernate not able to infer this field will be given a default value on database side? I believe #DynamicInsert is not even need if I understand it correctly.
You can simply initialize the variable and it sets that value as the default.
#Column(nullable = false)
private Boolean isProtected = false;
Else, you can use columnDefinition property of the #Column annotation, for example:
#Column(columnDefinition=“boolean default false")
private Boolean isProtected;

Hibernate upgrade from 3 to 4: Criteria transaction ceases to work

I have recently upgraded Spring 2.5 to 3.2 and Hibernate 3 to 4.2.8 in a general upgrade of a web application. Most things are working now, but there is one Criteria transaction that is not working and has me puzzled. The new version returns no result (but no errors), while the old one retrieved properly the requested value.
The code is the same one in the old and new versions, and I have verified that the argument that reaches it is the same. Here is the Java code:
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ViewingResource.class);
criteria.createCriteria("viewings","currentViewings");
criteria.add(Property.forName("currentViewings.id").eq(viewingId));
ViewingResource result = (ViewingResource)criteria.uniqueResult();
ViewingResource is my entity, which is defined as follows:
#Entity
#DiscriminatorValue("viewing")
public class ViewingResource extends AbstractInformationResource {
private static final long serialVersionUID = -4569093742552159052L;
#OneToOne(targetEntity = Attribute.class, fetch = FetchType.LAZY)
#JoinColumn
private Attribute primaryAttribute;
#OneToMany(targetEntity = Viewing.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, orphanRemoval=true)
#Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
#JoinTable(name = "informationresource_viewings")
#OrderBy("sort")
private Set<ResourceViewing> viewings;
public Set<ResourceViewing> getViewings() {
return viewings;
}
public Attribute getPrimaryAttribute() {
return primaryAttribute;
}
}
As for the abstract class it extends:
#Entity
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(
name = "type",
discriminatorType = DiscriminatorType.STRING
)
#Table(name = "informationresource")
abstract public class AbstractInformationResource extends PersistentEntity<String> {
private static final long serialVersionUID = 8709376067232042462L;
#Id #GeneratedValue(generator="system-uuid")
#GenericGenerator(name="system-uuid", strategy = "uuid")
private String id;
#Column(nullable = false)
private String name;
#Column(nullable = false)
private int sort;
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getSort() {
return sort;
}
}
And the original PersistentEntity is just an extension of Serializable with an id and no annotations.
I enabled Hibernate logs and found the problem may be in the way annotations work between in Hibernate 3 and 4, for the Hibernate generated SQL strings differ in this way:
Hibernate 3:
select
... (maps to all columns)
from
informationresource this_
inner join
informationresource_viewings viewings3_
on this_.id=viewings3_.informationresource_id
inner join
Viewing currentvie1_
on viewings3_.viewings_id=currentvie1_.id
where
this_.type in (
'viewing', 'directory'
)
and currentvie1_.id=?
Whereas in Hibernate 4, the generated SQL performs no joins:
select
... (maps to all columns, except type, attributeType and fieldName)
from
informationresource this_,
informationresource_viewings viewings3_,
Viewing currentvie1_
where
this_.id=viewings3_.informationresource_id
and viewings3_.viewings_id=currentvie1_.id
and this_.type='viewing'
and currentvie1_.id=?
Any hints that may help me advance with this issue? My current guess is that maybe I skipped some annotation definition that has been changed or modified since Hibernate 3, but so far I haven't been able to find anything illegal in the way I declare them - and my attempts to modify the #Join have been unsuccessful so far.
EDIT. After toying with this for some time, I have found that the issue may be related to the #DiscriminatorColumn of the abstract class. I have found that the problem lies that my type for this kind of request is never 'viewing', but 'directory'. In the old generated SQL I had both types generated:
this_.type in (
'viewing', 'directory'
)
But in the new sql this is constrained to 'viewing':
and this_.type='viewing'
I have changed in the new SQL this line, and it returns the right values that I need. The column type has only those two values, 'viewing' and 'directory'. So my question now is how to make Criteria to keep asking for the types there instead of forcing 'viewing' type.
Finally I found the solution, thanks to the hint I appointed in the EDIT block.
The solution came from establishing a formula in the base class:
#DiscriminatorFormula("case when type in ('viewing','directory') then 1 else 2 end")
And then changing in viewing resource the discriminator value annotation:
#DiscriminatorValue("1")
I really don't know why in Hibernate 3 I got all the values in the discrimination, and in Hibernate 4 the value was only this one, since the code had not changed at all. So if anyone in the future sees some similar behavior, maybe this trick can help you.

Can JPA be persuaded to convert between eg UUIDs and Strings?

I have a Java object with a field which is a UUID. I'd like to be able to persist this object to the database in the obvious way; however, the Basic mapping will use Java serialization to write it, while I want the UUID to be present in its obvious string form. Is there a way to supply a UUID <-> String converter to JPA for that field which will be used at read and write time so I can handle this type naturally?
Chris Lercher commented Note: Starting from JPA 2.1, a #Convert annotation can be used with an AttributeConverter<UUID, String>.
This approach works well and is compatible with any JPA provider, whereas the #Type(type = "uuid-char") is provider specific.
Also, with autoApply=true is applied to every field of every entity, so there is no need to annotate each field in each entity. See the documentation here and check the example below:
The converter class
#Converter(autoApply = true)
public class UuidConverter implements AttributeConverter<UUID, String> {
#Override
public String convertToDatabaseColumn(final UUID entityValue) {
return ofNullable(entityValue)
.map(entityUuid -> entityUuid.toString())
.orElse(null);
}
#Override
public UUID convertToEntityAttribute(final String databaseValue) {
return ofNullable(databaseValue)
.map(databaseUuid -> UUID.fromString(databaseUuid))
.orElse(null);
}
}
The entity
#Entity
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
#Column
private String name;
#Column(nullable = false, unique = true, updatable = false, columnDefinition="CHAR(36)")
private UUID customerId = randomUUID();
//.....
}
And this is how it looks in the database
TABLE customer
ID BIGINT(19) NO PRI (NEXT VALUE FOR SYSTEM_SEQUENCE_5D3)
NAME VARCHAR(255) YES NULL
CUSTOMER_ID VARCHAR(36) NO UNI NULL
JPA 2.0 doesn't provide a general way to do it, except for creating separate getters/setters for different representations of the same field.
Depending on your JPA provider you can use implementation-specific methods, for example, Hibernate provides a uuid-char type for this purpose:
#Type(type = "uuid-char")
private UUID uuid;
You could annotate your UUID property #Transient and at the same time provide its persistable String-based representation.
During #PrePersist, #PreUpdate or #PostLoad you'll set this String-based representation basing on UUID or (in case of loading it from the DB) re-create your UUID from the String.
I am not aware of the JPA itself. There is a way using Hibernate if it is your ORM provider.
Hibernate 5.x
Use the #Type annotation with "uuid-char" to map into the VARCHAR column.
#Type(type="uuid-char")
private UUID uuid;
Documentation: JBoss Hibernate 5.6 User Guide | 2.3. Basic types
Hibernate 6.x
As of Hibernate 6.x, you can use the following configuration to map them automatically:
hibernate.type.preferred_uuid_jdbc_type=CHAR
Documentation: JBoss Hibernate 6.2 User Guide | 2.2.38. UUID

Hibernation annotations, specify column default value

I have a domain object and annotated as follows
#Entity
#Table(name = "REQUEST")
public class Request {
/**
* Unique id for this request
*/
#Id
#GeneratedValue
#Column(name = "EQ_ID")
private long requestId;
/**
*
*/
#Column(name = "EMAIL_ID")
private String emailId;
/**
*
*/
#Column(name = "REQUEST_DATE")
private Date requestDate;
/**
*Getters/setters omitted
*/
}
The column Request_date cannot be null and as per the DDL the default value is sysdate (oracle DB). How do I annotate this field so that if the requestDate property is null,hiberanate automatically inserts sysdate.? Currently it throws error when the field is null,which is very obvious as it cannot be null as per the DB constraints. How do I go about this?
One alternative is to mark this field as transient and the inserts work fine. But the negative aspect is that, I will not be able to retrieve the value (of request_date column).
This is a missing feature in hibernate annotations. Also there exist some workaround as Yok has posted. The problem is that the workaround is vendor dependent and might not work for all DB. In my case,Oracle, it isn't working and has been reported as a bug.
You can put the default value in a columnDefinition. An example would look like:
#Column(name = "REQUEST_DATE", nullable = false, columnDefinition = "date default sysdate")
Using #ColumnDefault (Work for DDL update).
hibernate.hbm2ddl.auto=update
import org.hibernate.annotations.ColumnDefault;
....
#ColumnDefault(value="'#'")
#Column(name = "TEMP_COLUMN", nullable = false)
public String getTempColumn() {
return tempColumn;
}
DDL Generate:
Alter Table YOUR_TABLE add TEMP_COLUMN varchar2(255) default '#' not null;
Assign a default value to the field:
private Date requestDate = new Date();
If you mark your entity with #DynamicInsert e.g.
#Entity
#DynamicInsert
#Table(name = "TABLE_NAME")
public class ClassName implements Serializable {
Hibernate will generate SQL without null values. Then the database will insert its own default value. This does have performance implications See Dynamic Insert.
Make the default in Oracle for the column SYSDATE:
ALTER TABLE APP MODIFY (REQUEST_DATE DEFAULT SYSDATE);
Then, from Hibernate's perspective it can be nullable.
Hibernate will save a NULL to the database. Oracle will convert that to SYSDATE. And everyone will be happy.
I resolved assigning a value to the variable like this private Integer active= 0;
#Entity
#Table(name="products")
public class ServiziTipologia {
#Id
#GeneratedValue
private Integer id;
private String product;
private String description;
private Integer active= 0;

How to set a default entity property value with Hibernate

How do I set a default value in Hibernate field?
If you want a real database default value, use columnDefinition:
#Column(name = "myColumn", nullable = false, columnDefinition = "int default 100")
Notice that the string in columnDefinition is database dependent. Also if you choose this option, you have to use dynamic-insert, so Hibernate doesn't include columns with null values on insert. Otherwise talking about default is irrelevant.
But if you don't want database default value, but simply a default value in your Java code, just initialize your variable like that - private Integer myColumn = 100;
Use hibernate annotation.
#ColumnDefault("-1")
private Long clientId;
Recreate the table if it already exists for the changes to take effect.
You can use #PrePersist anotation and set the default value in pre-persist stage.
Something like that:
//... some code
private String myProperty;
//... some code
#PrePersist
public void prePersist() {
if(myProperty == null) //We set default value in case if the value is not set yet.
myProperty = "Default value";
}
// property methods
#Column(nullable = false) //restricting Null value on database level.
public String getMyProperty() {
return myProperty;
}
public void setMyProperty(String myProperty) {
this.myProperty= myProperty;
}
This method is not depend on database type/version underneath the Hibernate. Default value is set before persisting the mapping object.
what about just setting a default value for the field?
private String _foo = "default";
//property here
public String Foo
if they pass a value, then it will be overwritten, otherwise, you have a default.
Default entity property value
If you want to set a default entity property value, then you can initialize the entity field using the default value.
For instance, you can set the default createdOn entity attribute to the current time, like this:
#Column(
name = "created_on"
)
private LocalDateTime createdOn = LocalDateTime.now();
Default column value using JPA
If you are generating the DDL schema with JPA and Hibernate, although this is not recommended, you can use the columnDefinition attribute of the JPA #Column annotation, like this:
#Column(
name = "created_on",
columnDefinition = "DATETIME(6) DEFAULT CURRENT_TIMESTAMP"
)
#Generated(GenerationTime.INSERT)
private LocalDateTime createdOn;
The #Generated annotation is needed because we want to instruct Hibernate to reload the entity after the Persistence Context is flushed, otherwise, the database-generated value will not be synchronized with the in-memory entity state.
Instead of using the columnDefinition, you are better off using a tool like Flyway and use DDL incremental migration scripts. That way, you will set the DEFAULT SQL clause in a script, rather than in a JPA annotation.
Default column value using Hibernate
If you are using JPA with Hibernate, then you can also use the #ColumnDefault annotation, like this:
#Column(name = "created_on")
#ColumnDefault(value="CURRENT_TIMESTAMP")
#Generated(GenerationTime.INSERT)
private LocalDateTime createdOn;
Default Date/Time column value using Hibernate
If you are using JPA with Hibernate and want to set the creation timestamp, then you can use the #CreationTimestamp annotation, like this:
#Column(name = "created_on")
#CreationTimestamp
private LocalDateTime createdOn;
If you want to do it in database:
Set the default value in database (sql server sample):
ALTER TABLE [TABLE_NAME] ADD CONSTRAINT [CONSTRAINT_NAME] DEFAULT (newid()) FOR [COLUMN_NAME]
Mapping hibernate file:
<hibernate-mapping ....
...
<property name="fieldName" column="columnName" type="Guid" access="field" not-null="false" insert="false" update="false" />
...
See, the key is insert="false" update="false"
One solution is to have your getter check to see if whatever value you are working with is null (or whatever its non-initialized state would be) and if it's equal to that, just return your default value:
public String getStringValue(){
return (this.stringValue == null) ? "Default" : stringValue;
}
Use #ColumnDefault() annotation. This is hibernate only though.
I searched for this and found many answers to default value for column.If you want to use default value defined in SQL Table then in #Column Annotation use "insertable = false". insertable
#Column(name = columnName, length = lengthOfColumn, insertable = false)
If you are using columnDefination it #Column annotation may be it won't work as it is Database dependent.
Working with Oracle, I was trying to insert a default value for an Enum
I found the following to work the best.
#Column(nullable = false)
#Enumerated(EnumType.STRING)
private EnumType myProperty = EnumType.DEFAULT_VALUE;
To use default value from any column of table. then you must need to define #DynamicInsert as true or else you just define #DynamicInsert. Because hibernate takes by default as a true.
Consider as the given example:
#AllArgsConstructor
#Table(name = "core_contact")
#DynamicInsert
public class Contact implements Serializable {
#Column(name = "status", columnDefinition = "int default 100")
private Long status;
}
You can use the java class constructor to set the default values. For example:
public class Entity implements Serializable{
private Double field1
private Integer field2;
private T fieldN;
public Entity(){
this.field1=0.0;
this.field2=0;
...
this.fieldN= <your default value>
}
//Setters and Getters
...
}
I tried it. when i did that
#Column(name = "is_sale", columnDefinition = "default false")
private boolean isSale = false;
he did not add. And when I did
#Column(name = "is_sale", columnDefinition = "bool default false")
private boolean isSale = false;
in this case Hibernate generated such sql
alter table if exists customer_product add column is_sale bool default false
and it helped me
<property name="age" type="integer">
<column name="age" not-null="false" default="null" />
</property>
i'am working with hibernate 5 and postgres, and this worked form me.
#Column(name = "ACCOUNT_TYPE", ***nullable***=false, columnDefinition="varchar2 default 'END_USER'")
#Enumerated(EnumType.STRING)
private AccountType accountType;
If you want to set default value in terms of database, just set #Column( columnDefinition = "int default 1")
But if what you intend is to set a default value in your java app you can set it on your class attribute like this: private Integer attribute = 1;
Suppose we have an entity which contains a sub-entity.
Using insertable = false, updatable = false on the entity prevents the entity from creating new sub-entities and preceding the default DBMS value. But the problem with this is that we are obliged to always use the default value or if we need the entity to contain another sub-entity that is not the default, we must try to change these annotations at runtime to insertable = true, updatable = true, so it doesn't seem like a good path.
Inside the sub-entity if it makes more sense to use in all the columns insertable = false, updatable = false so that no more sub-entities are created regardless of the method we use (with #DynamicInsert it would not be necessary)
Inserting a default value can be done in various ways such as Default entity property value using constructor or setter. Other ways like using JPA with columnDefinition have the drawback that they insert a null by default and the default value of the DBMS does not precede.
Insert default value using DBMS and optional using Hibernate
But using #DynamicInsert we avoid sending a null to the db when we want to insert a sub-entity with its default value, and in turn we allow sub-entities with values other than the default to be inserted.
For inserting, should this entity use dynamic sql generation where only non-null columns get referenced in the prepared sql statement?
Given the following needs:
The entity does not have the responsibility of creating new sub-entities.
When inserting an entity, the sub-entity is the one that was defined as default in the DBMS.
Possibility of creating an entity with a sub-entity which has a UUID other than the default.
DBMS: PostgreSQL | Language: Kotlin
#Entity
#Table(name = "entity")
#DynamicInsert
data class EntityTest(
#Id #GeneratedValue #Column(name = "entity_uuid") val entityUUID: UUID? = null,
#OneToOne(cascade = [CascadeType.ALL])
#JoinColumn(name = "subentity_uuid", referencedColumnName = "subentity_uuid")
var subentityTest: SubentityTest? = null
) {}
#Entity
#Table(name = "subentity")
data class SubentityTest(
#Id #GeneratedValue #Column(name = "subentity_uuid", insertable = false, updatable = false) var subentityUUID: UUID? = null,
#Column(insertable = false, updatable = false) var name: String,
) {
constructor() : this(name = "")
}
And the value is set by default in the database:
alter table entity alter column subentity_uuid set default 'd87ee95b-06f1-52ab-83ed-5d882ae400e6'::uuid;
GL
Source 1
Source 2
we can have getter that annotates #Column
all #column should be annotated in getter alone instead of direct variable declaration. by this way, we can resolve it.
#Column(name = "string_value")
public String getStringValue(){
return (this.stringValue == null) ? "Default" : stringValue;
}

Categories