Solved

Java temporal types,JDBC temporal types, Enumerated types,Serializable objects, Transient fields used in EJB

Posted on 2011-09-06
5
500 Views
Last Modified: 2012-05-12
I would like to know Java temporal types,JDBC temporal types, Enumerated types,Serializable objects, Transient fields used in EJB. When, why, how, where we use them.Advantages, disadvantages of using them.  I was not clear on this concept. Thanks in advance
0
Comment
Question by:gudii9
  • 3
  • 2
5 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
Comment Utility

This is about  @Temporal from
http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes

Temporal, Dates, Times, Timestamps and Calendars

Dates, times, and timestamps are common types both in the database and in Java, so in theory mappings these types should be simple, right? Well sometimes this is the case and just a normal Basic mapping can be used, however sometimes it becomes more complex.

Some databases do not have DATE and TIME types, only TIMESTAMP fields, however some do have separate types, and some just have DATE and TIMESTAMP. Originally in Java 1.0, Java only had a java.util.Date type, which was both a date, time and milliseconds. In Java 1.1 this was expanded to support the common database types with java.sql.Date, java.sql.Time, and java.sql.Timestamp, then to support internationalization Java created the java.util.Calendar type and virtually deprecated (almost all of the methods) the old date types (which JDBC still uses).

If you map a Java java.sql.Date type to a database DATE, this is just a basic mapping and you should not have any issues (ignore Oracle's DATE type that is/was a timestamp for now). You can also map java.sql.Time to TIME, and java.sql.Timestamp to TIMESTAMP. However if you have a java.util.Date or java.util.Calendar in Java and wish to map it to a DATE or TIME, you may need to indicate that the JPA provider perform some sort of conversion for this. In JPA the @Temporal annotation or <temporal> element is used to map this. You can indicate that just the DATE or TIME portion of the date/time value be stored to the database. You could also use Temporal to map a java.sql.Date to a TIMESTAMP field, or any other such conversion.
[edit] Example of temporal annotation

@Entity
public class Employee {
    ...
    @Basic
    @Temporal(DATE)
    private Calendar startDate;
    ...
}

[edit] Example of temporal XML

<entity name="Employee" class="org.acme.Employee" access="FIELD">
    <attributes>
        ...
        <basic name="startDate">
            <temporal>DATE</temporal>
        </basic>
    </attributes>
</entity>

0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
what are differences between java.util.Date and java.sql.Time. When do we use each of them. please advise. for me it seems using java.sql.Time is simple and straight forward. In that case when do i need to use java.util.Date or java.util.Calendar. please advise
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
why Date is created under java.util but not under java.sql which surprises me. please advise
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
raed this, I believe it has anextended response this question:
http://stackoverflow.com/questions/2305973/java-util-date-vs-java-sql-date

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Calendar, GergorianCalendar are newer classes which deal
much more cautiously with many subtle features
of operatiins with dates, espceailly those related tio TimeZones, differences between say standard time and daylight saving time, etcetc.
In my mind, these Claendars are less intuitive and more difficult to rememember - all rthe time
you need to checvk APIs
So if you are dealing with appliactionswhen time zones are some other subtle points are really crucial, you want
to use Calendar's. In practice at least with me it happend very rarely, so
dealing with Dates is usually fine
 
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now