Solved

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

Posted on 2011-09-06
5
511 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
ID: 36492934

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
ID: 36589658
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
ID: 36589689
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
ID: 36589723
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
ID: 36589753
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 39
hibernate insert example 13 29
String array comparison 4 34
jsp insert to database example 2 26
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

823 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