What does Hibernate map a boolean datatype to when using an Oracle database by default? - oracle

By default if I create a field in an entity like:
#NotNull
boolean myBoolean;
And I let Hibernate auto-create my tables. What Oracle data type will this map to?

As #Arthur said it maps to Number(1) which would be the standard sql bit where 0 == false and 1 == true. As an alternative you can map char(1) to 'T' or 'F' like this
#org.hibernate.annotations.Type(type="true_false")
#NotNull
boolean myBoolean;
or map it to 'Y' or 'N'
#org.hibernate.annotations.Type(type="yes_no")
#NotNull
boolean myBoolean;

Simply Number(1)
If you want, use SchemaExport to generate a script to your target database. Something like
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration
.addAnnotatedClass(<TYPE_YOUR_CLASS>.class)
.setProperty(Environment.USER, <TYPE_YOUR_USER>)
.setProperty(Environment.PASS, <TYPE_YOUR_PASSWORD>)
.setProperty(Environment.URL, <TYPE_YOUR_URL>)
.setProperty(Environment.DIALECT, <TYPE_YOUR_DIALECT>)
.setProperty(Environment.DRIVER, <TYPE_YOUR_DRIVER>);
SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile("schema.sql");
schema.create(<DO_YOU_WANT_TO_PRINT_TO_THE_CONSOLE>, <DO_YOU_WANT_TO_EXPORT_THE_SCRIPT_TO_THE_DATABASE>);

This is what you really need
Java POJO:
//#Type(type="true_false") //not working for '1' and '0' in NUMERIC(1) field
#Type(type= "org.hibernate.type.NumericBooleanType")
#NotNull(message="NOT_NULL")
#Column(name = "IS_DELEGATION", nullable = false)
private Boolean isDelegation;
Oracle DDL
alter table agent add (is_delegation number(1) default 0 not null);
As stated in
Hibernate docu

Related

Sorting in Java | How to get value from other field of entity if the current field is null while sorting entity list in java?

Context :
Consider following entity in spring boot project,
#Entity
public class Transaction{
#Id
private Integer tId;
private Integer amount;
private LocalDate invoiceDate;
private LocalDate tDate;
}
A method which creates object of transaction, doesn't set tDate.
There is an independent method which sets tDate.
So, in the database some entries don't have 'tDate'.
Problem :
Now, I want to show all the entries in database sorted using tDate but, for those entries which does not contain tDate it should consider invoiceDate.
Sample Database entries
t_id
amount
invoice_date
t_date
1
1200
2/3/2022
4/3/2022
2
2434
5/3/2022
6/3/2022
3
234
3/3/2022
NULL
Sample expected Output
[[tId = 3, amount = 234, invoiceDate = 3/3/2022, tDate = NULL]
[tId = 1, amount = 1200, invoiceDate = 2/3/2022, tDate = 4/3/2022]
[tId = 2, amount = 2434, invoiceDate = 5/3/2022, tDate = 6/3/2022]]
Note : Highlighted dates above are the dates considered for sorting.
What I tried
I tried to use the combination of nullsLast(Comparator.naturalOrder()) and thenComparing() methods of Comparator.comparing() but it doesn't give the expected output. It sorts the entries withtDate and entries without tDate separately.
Thank you in advance for any help!!
Also, I'm using MongoRepository in repository layer, so I can use the Sort object as well.
Thank you!

Postgres Timestamp column with Java util.Date

I have a (start_time) column which is of type timestamp with time zone. and I have a JPA mapping entity which reads as below,
#Column(name = "start_time")
#Temporal(TemporalType.TIMESTAMP)
private Date startTime;// (util.date)
My table has value "2017-08-09 09:57:00+05:30". When I use JPA to map the values, I get the value as "2017-08-09 09:57:00.0"
when I try to debug, I can see CDATE inside the date attribute which shows me the correct value with timezone. How to map this correctly?
Try to change #Column(name = "start_time") on #Column(name = "start_time", columnDefinition = "TIMESTAMP WITH TIME ZONE")

Getting MinDate instead of NULL

I am calling a stored procedure, and may expect a NULL back.
However, when I get to EntityFramework with the result, it seems that the result gets converted to a Min Date (01-01-0001).
result.NextPaymentDate =
(from c in Context.GetPaymentDatesForSchedule(source.id) where c.NextPaymentFlag select c.PaymentDate)
.FirstOrDefault();
Is there a way to make it NULL, if I get a NULL value from the sproc?
It seems 'dirty' doing something like:
if (result.NextPaymentDate == DateTime.MinValue)
result.NextPaymentDate = null;
It is because the NextPaymentDate property is not nullable in your model, if you make it nullable you will then get the null value instead of min date.

org.hibernate.MappingException :No Dialect mapping for JDBC type: -9 [duplicate]

This question already has answers here:
JPA SQL Server No Dialect mapping for JDBC type: -9
(5 answers)
Closed 3 years ago.
When I am trying to fetch the record from a table in SQLServer2008 am getting the exception called: org.hibernate.MappingException :No Dialect mapping for JDBC type: -9 why?
Although the config file is correct.
It's the problem of hibernate mapping types.
you can extends a dialect.
eg:
public class SQLServerDialectOverrider extends SQLServerDialect{
public SQLServerDialectOverrider() {
super();
registerHibernateType(Types.NVARCHAR, Hibernate.STRING.getName());
registerHibernateType(Types.LONGVARCHAR, Hibernate.TEXT.getName());
}
}
Use this class as the dialect class.
I changed the query and explicitly cast it to varchar and it worked.......
String myquery = "select cast(t2.name as varchar) column_name from sys.objects t1 inner join sys.columns t2 on t2.object_id = t1.object_id"+
" left join sys.indexes t3 on t3.object_id = t1.object_id and t3.is_unique = 1 left join sys.index_columns t4 on t4.object_id = t1.object_id and t4.index_id = t3.index_id and t4.column_id = t2.column_id where (upper(t1.type) = 'U' or upper(t1.type) = 'V') and upper(schema_name(t1.schema_id)) = 'dbo' and upper(t1.name) = 'TEST'";
This issue occurs if you are accessing the database table data in the different format on which its store in database
Example:
in data base column type is "nvarchar" and you are accessing data as a string via hibernate.
To avoid this you can add scalar in hibernate which will explicitly cast the column data as per requirement. scalar can be added in java while executing the query also in the hbm.xml file.

LINQ query - null date

Ive a simple linq query to return records with a null date field,
just want to check the synatx of the "where" line is ok
var query2 = from cs in db.tblCases
where cs.date_closed == null
etc, etc,
thanks again
DD
I would be careful with using null, I have seen issues with linq not generating the correct sytnax (ex IS NULL vs ==null)
I would recommend
var query2 = from cs in db.tblCases where !cs.date_closed.HasValue etc, etc,
Assuming your date_closed property is of a nullable type, e.g. Nullable<DateTime> aka DateTime?, that should be fine.

Resources