DAO pattern and Java Persistence API

Hi
This is a question for anyone who might be familiar with the standard DAO design pattern and the Java Persistence API (JPA - part of EJB3). I'm new to this technology, so apologies for any terminology aberrations.
I am developing the overall architecture for an enterprise system. I intend to use the DAO pattern as the conceptual basis for all data access - this data will reside in a number of forms (e.g. RDBMS, flat file). In the specific case of the RDBMS, I intend to use JPA. My understanding of JPA is that it does/can support the DAO concept, but I'm struggling to get my head around how the two ideas (can be made to) relate to each other.
For example, the DAO pattern is all about how business objects, data access objects, data transfer objects, data sources, etc relate to each other; JPA is all about entities and persistence units/contexts relate to each other. Further, JPA uses ORM, which is not a DAO concept.
So, to summarise - can DAO and JPA work together and if so how?
Thanks
P.S. Please let me know if you think this topic would be more visible in another forum (e.g. EJB).

Thanks, duffymo, that makes sense. However ... having read through numerous threads in which you voice your opinion of the DAO World According to Sun, I'd be interested to know your thoughts on the following ...
Basically, I'm in the process of proposing an enterprise system architecture, which will use DAO as the primary persistence abstraction, and DAO + JPA in the particular case of persistence to a RDBMS. In doing so, I'd like to illustrate the various elements of the DAO pattern, a la the standard class diagram that relates BusinessObject / DataAccessObject / DataSource / TransferObject (http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html). With reference to this model, I know that you have a view on the concept of TransferObject (aka ValueObject?) - how would you depict the DAO pattern in its most generic form? Or is the concept of a generic DAO pattern compromised by the specific implementation that is used (in this case JPA)?

Similar Messages

  • Object, Query and Java Persistence API

    Hello Java Programmers.
    Question to Object handling with Query results and evaluation.
    Example:
    one table with 3 Columns
    Tablename = ISO6933
    Column 1 = UID
    Column 2 = language name
    Column 3 = language long name
    data in database:
    C1 = 1
    C2 = "ger"
    C3 = "german"
    read the data from database with EntityManagerFactory and EntityManager
    Query query = em.createNativeQuery("SELECT * FROM ISO6933");Final to - Query (Persistence) to Choice (AWT)
    Search short code for example!
    query.setFirstResult(0); // first element -> possible?
    java.util.List queryList = query.getResultList();
    // warnings -> List is a raw type. References to generic type List<E> should be parameterized
    java.awt.List list = new List();
    list.add(queryList.toString()); // for one element in the result, for more elements for/next loop
    // = [1,ger,german]I do not want to use
    - StringTokenizer
    - String.split
    (Cast from java.util.List to java.awt.List = Type mismatch: cannot convert from List to List)
    for one elements from [1,ger,german]
    final to
    1
    ger
    german
    in the AWT List()

    no answer? = is an answer !
    the normal solution would be for example.
         Query query = em.createNativeQuery("SELECT * FROM ISO693_3"); // create query
              query.setFirstResult(0); // first element          
              Object[] objectList = query.getResultList().toArray();          
              // objectList[X] = UID,LANGUAGE_ID,LANGUAGE_NAME
              int intLength = objectList.length; // Length          
              StringTokenizer token; // parser
              String string = new String(); // temp
              for (int i1=0;i1<intLength;i1++)
                   string = objectList[i1].toString();
                   string = string.substring(1, string.length()-1);
                   token = new StringTokenizer(string, ",");
                   while (token.hasMoreElements())
                        list.add(token.nextToken().trim()); // the next element and clean string
              }I look for however a better solution!
    Best greetings

  • Java Persistence API and creation of databases

    Hi All
    I have designed a small address book application which uses a database to store the information. I have used the new Java Persistence API to take care of my transactions and querrys with the database. This works great for one set database which is generated the first time the application runs and is used all the time.
    However I would like to make it so that the user can create a new address book (database) with a different name, I am not sure how I would do this using the Java Persistence API. I know the persistence.xml file must be placed in the META-INF directory of the source, so how would I be able manipulate the file or could I place the file somewhere else.
    David

    I am using the Derby database. I can create a database by passing the create command when I create the entity Manager like so:
    Connection c = DriverManager.getConnection("jdbc:derby:AddressBook;create=true", "app", "app");My problem is that as I understand it the persistence.xml file must know the name of the database. Unless there is another way of setting this. The line in the persistence file I am referring to is:
    <property name="toplink.jdbc.url" value="jdbc:derby:AddressBook"/>I wonder if I can progamatically set this value so that I have a default database in the persistence.xml and others can then create there own.
    David

  • Newbie question on Java Persistence API - Entity Beans

    Hi All,
    I am basically new to Entities and the Java Persistence API. My question is, when using a container managed EntityManager, do I have to manually tidy-up any resources? Say for example, do I have to explicitly close the database connection (if ever I have that ability)? Invoke close() on EntityManager?
    - Pat

    You don't have to. That's what they mean by container managed. The container does it for you.
    In fact you will get an IllegalStateException if you call close on a container-managed EntityManager.

  • Question about Java Persistence API.

    is Java Persistence API a Object-Relational Mapping Framework, or the java Programmer can use this API to do mapping between Java Applicatiion and Relational Database ??

    http://java.sun.com/javaee/overview/faq/persistence.jsp

  • Java persistence api : ejb ql

    Hi,
    I am using java persistence api(over oracle toplinks)
    I have an entity customer and another entity contacts.
    customer is mapped with contact as one to many.
    the contact have a field primarycontact.
    I want to get the contact name from contacts for a customer where primarycontact field is true. and set this contact name as a field customercontact which is a string in customer..
    I got a way by executing multiple queries.
    but is it possible to do this in single query using ejb ql?
    Any help will be greatly appreciated..
    Thanks in advance....

    @Id
    @GeneratedValue( @GeneratedValue(strategy= GenerationType.TABLE, generator="SOMENAME")
    public int setId() {
    returrn this.id;
    in the database you need to create table using following commands
    CREATE TABLE SEQUENCE
    SEQ_NAME VARCHAR(50),
    SEQ_COUNT DECIMAL(15)
    and insert a row initally with the same "SOMENAME" like
    insert into sequence values ( "SOMENAME", 1);
    you can get the next id by adding 50(default) to "SELECT SEQ_COUNT FROM SEQUENCE WHERE SEQ_NAME = "SOMENAME"
    hope this provides a solution.

  • DAO Pattern and the ServiceLocator

    I have been developing a lightweight framework for working with AIR and the SQL API. This framework has no dependencies on Cairngorm however it is built around a typical DAO implementation.
    While developing this I considered how it should be integrated with Cairngorm, which lead me to wonder if a simple DAO marker interface which could be retrieved from the ServiceLocator (and cast to the correct abstraction by a business delegate) would not be all that is needed to have a rather flexible service layer in Cairngorm?
    For example, consider the following pseudo code which is what I would imagine a business delegate to look like:
    class FooDelegate {
    protected fooDAO:IFooDAO;
    public FooDelegate(responder:IResponder) {
    fooDAO = ServiceLocator.getinstance().getDAO(Service.FOODAO) as IFooDAO;
    fooDAO.addResponder( responder );
    public getFoo() void {
    fooDAO.getFoo();
    public addFoo(foo:IFoo) {
    fooDAO.addFoo(foo);
    public deleteFoo(foo:IFoo) {
    fooDAO.deleteFoo(foo);
    As you can see the delegate would cast the dao to the correct abstraction and then just wrap the DAOs API (somewhat similar to an Assembler in LCDS).
    A custom DAO interface would extend the DAO marker interface so that the ServiceLocator could find it:
    interface IFooDAO extends IDAO
    getFoo() void;
    addFoo(foo:IFoo);
    deleteFoo(foo:IFoo);
    Service.mxml would define an instance of the dao as an abstraction and then call a factory to get the appropriate implementation:
    public fooDAO:IFooDAO = DAOFactory.getDAO("foo");
    I see much potential in this type of implementation as it would allow services to be swaped out with different implementations via a config or with an IoC implementation etc, thus allowing the services themselves to be completely transparent to client code.
    I wanted to see if anyone had any thoughts on this as well?
    Best,
    Eric

    Thanks, duffymo, that makes sense. However ... having read through numerous threads in which you voice your opinion of the DAO World According to Sun, I'd be interested to know your thoughts on the following ...
    Basically, I'm in the process of proposing an enterprise system architecture, which will use DAO as the primary persistence abstraction, and DAO + JPA in the particular case of persistence to a RDBMS. In doing so, I'd like to illustrate the various elements of the DAO pattern, a la the standard class diagram that relates BusinessObject / DataAccessObject / DataSource / TransferObject (http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html). With reference to this model, I know that you have a view on the concept of TransferObject (aka ValueObject?) - how would you depict the DAO pattern in its most generic form? Or is the concept of a generic DAO pattern compromised by the specific implementation that is used (in this case JPA)?

  • Java persistence API

    Hi! I am trying to learn to use the http://www.devx.com/Java/Article/33650/ api manually, i tried using netbeans 6.0 for it but i'm not very sure what it does so i am going to try it manually now. I've read these 2 articles by Antonio Goncalves
    (http://www.devx.com/Java/Article/33650/ && http://www.devx.com/Java/Article/33906/) it doesn't look very difficult at first but the thing is in netbeans 6.0 when i try to use the annotations,@Inheritance for example, It autocompletes but i don't see the javadoc it says ("Javadoc not found"). I already added the javaee5 documentation to the javaplataform manager but it doesn't do the trick.
    Anybody can help? Also if you know any kind of tutorial for the persistence api if you could point me towards there it would be nice.
    And finally sorry if i have misplaced this post, I couldn't find a better place.
    Thanks, Joaquin

    Hi! I am trying to learn to use the http://www.devx.com/Java/Article/33650/ api manually, i tried using netbeans 6.0 for it but i'm not very sure what it does so i am going to try it manually now. I've read these 2 articles by Antonio Goncalves
    (http://www.devx.com/Java/Article/33650/ && http://www.devx.com/Java/Article/33906/) it doesn't look very difficult at first but the thing is in netbeans 6.0 when i try to use the annotations,@Inheritance for example, It autocompletes but i don't see the javadoc it says ("Javadoc not found"). I already added the javaee5 documentation to the javaplataform manager but it doesn't do the trick.
    Anybody can help? Also if you know any kind of tutorial for the persistence api if you could point me towards there it would be nice.
    And finally sorry if i have misplaced this post, I couldn't find a better place.
    Thanks, Joaquin

  • Isolation level 0 and java persistence frameworks

    Hi,
    i use www.castor.org as my persistence framework, castor have have its own lock engine and cache for entities, then i think to avoid some table lock in database to pass for JDBC connection the parameter isolation=0, as any update or write is made and controlled by castor in castor i dont have dirty data, anyone have experience with any other java persistence engine and used maxdb with lock engine managed by framework?
    any insight about this are welcome.
    best regards
    Clóvis

    > Hi,
    >
    > i use www.castor.org as my persistence framework, castor have have its own lock engine and cache for entities, then i think to avoid some table lock in database to pass for JDBC connection the parameter isolation=0, as any update or write is made and controlled by castor in castor i dont have dirty data, anyone have experience with any other java persistence engine and used maxdb with lock engine managed by framework?
    >
    Sure
    The whole SAP NetWeaver stack runs with isolation level 0.
    Works quite fine.
    In fact there are certain features (like the count(*) optimization) that don't work consistently with isolation level 1 (read commited).
    So you shouldn't get into trouble with that.
    regards,
    Lars

  • Hibernate, DAO pattern and tree hierarchy

    Hi all,
    I use Hibernate for a short period of time and now I'm facing a complex problem . I try figure it out what is the best practice for the following scenario:
    I have the following classes: Department, Team, Position, all of them inherited from a Entity class even there is almost no difference between them. But I wanted different classes for different entities.
    I try to create a tree hierachy, each object is with all others in a bidirectional one-to-many relationship. For example a Department can have Teams and Positions as children and a Position can have Departments and Teams as children.
    I created the mapping files and I don't know how to create all necessary methods without duplicating the code.
    Questions:
    1. Do I need a DAO pattern implemented for this design?
    2. Can you recomend some documentation or ideas that will help me find out what is the best approach in this case?
    Thanks

    Write the DAO for the class that is the root of the tree. Sounds like it should be DepartmentDao.
    I don't know of much better documentation than the Hibernate docs. Check their forum, too.
    %

  • JPA, java persistence Api, troubleshooting

    Hi everybody.
    I have a small but boring problem.
    I am new at Netbeans and Java Sdk.
    I downloaded Netbeans 6.1 and SDK 6.
    I tried to exercise "simple database application" in the netbeans web page.
    A error encountered that:
    24.Tem.2008 10:04:16 org.jdesktop.application.Application$1 run
    SEVERE: Application class simpleAp.simpleAp failed to launch
    *javax.persistence.PersistenceException: No Persistence provider for EntityManager named simpleAppPU*: The following providers:
    oracle.toplink.essentials.PersistenceProvider
    oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
    Returned null to createEntityManagerFactory.
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
    I can't fix this. anybody know the solution??
    hanks, good work.

    Assalamu Alaikum,
    Check in persistence.xml whether persistence-unit is same in Persistence.createEntityManagerFactory*("simpleAppPU")* or not. It should be same.
    persistence.xml is like as follows:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="*simpleAppPU*" transaction-type="RESOURCE_LOCAL">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <class>Users</class>
    <properties>
    <property name="toplink.jdbc.user" value="root"/>
    <property name="toplink.jdbc.password" value="password"/>
    <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/innosched"/>
    <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
    </properties>
    </persistence-unit>
    </persistence>

  • Patterns in java imageio API

    Hello,
    Can somebody help me in figuring out 'Patterns' used in creating the javax.imageio API. By patterns I mean the Gang of Four Patterns like singleton, Decorator, Chain of responsibility....
    Please tell me if you know of any pattern used in this API.
    Thanks a lot for your help,
    Ganesh

    Dennis,
    I am creating a list of patterns used by the imageio API. Yes, this is indeed an assignment. I am not proficient in patterns. Any help in pointing the existing patterns will help me.
    Thanks,
    Ganesh

  • Java Persistence API + mySQL and East Europe characters

    Hi,
    I use Sun Application Server 9.0 and mySQL 4.1.20 on Linux. When I put data (with jsf form) into my database table everything is displayed correctly until I restart application server (or redeploy my web app). After restarting server non-ascii characters look strange. I suppose that there is a problem with character coding (between apps server and database) but I can't find any solution. I use UTF-8 character coding both in database fields and for displaying jsf pages.
    There are't any problems when I use Derby database for starage data.
    Thanks

    Can you give some more information ?
    Are you using netbeans 5.5 or doing this program standalone and interfacing
    directly with appserver ?
    Can you send a pointer to a gif of the incorrect looking characters ?
    Can you elaborate about putting data using jsf form and some details
    or extract from the code that writes or reads the data ?
    Do the names of your table or columns have the european characters
    in them or is it just the data ?
    Thanks for your help.
    >
    Thanks
    Hi,
    I use Sun Application Server 9.0 and mySQL 4.1.20 on
    Linux. When I put data (with jsf form) into my
    database table everything is displayed correctly
    until I restart application server (or redeploy my
    web app). After restarting server non-ascii
    characters look strange. I suppose that there is a
    problem with character coding (between apps server
    and database) but I can't find any solution. I use
    UTF-8 character coding both in database fields and
    for displaying jsf pages.
    There are't any problems when I use Derby database
    for starage data.
    Thanks

  • Best way to combine Java Persistence API (JPA)  with Visual Web Pack (VWP)?

    I like the JPA/Hibernate development approach to create the database elements from JAVA classes.
    This way one gets rid of the need to keep JAVA code and the database in sync.
    However I have not yet found a good way on how to use JPA from the netbeans VWP.
    Note that I have found the tutorial on how to use the IDE with hibernate and it works without problems.
    However I think this tutorial does not really implement a good Model/View/Controller approach as it is done in the enterprise pack.
    Here you have a controller session bean that deals with all the persistence logic and that exposes properties through a DataModel to the application.
    There is no need to encapsulate logic in inherited classes of the ObjectListDataProvider.
    Maybe an example makes it more clear:
    If we stay with the hibernate tutorial that we have "Animals" and "Pavillion" entities.
    How would one efficiently design a "Table" component that shows a list of all Animals and their Pavillions.
    With the VWP approach it is quite clear: you create a RowSet with the inner/left join between Animals and Pavillions.
    But how would you do it with JPA?

    You said:
    "If we stay with the hibernate tutorial that we have "Animals" and "Pavillion" entities.
    How would one efficiently design a "Table" component that shows a list of all Animals and their Pavillions.
    With the VWP approach it is quite clear: you create a RowSet with the inner/left join between Animals and Pavillions.
    But how would you do it with JPA?"
    I have the exact same question. Have you found the answer on how to "join" entities and display them on visual web table component?

  • Jakarta POI and Java Excel API

    I recently downloaded both these as I'm trying to create an application that reads information from an excel file. However, the problem I am having is that I just don't know where to start. When I download the files and unzip them, I haven't a clue where to put the files or how to call them from my java files. I've read the help files on both the websites but am no clearer on what to do. Any help would be much appreciated.

    Download an IDE like Eclipse or JBuilder. Set-up the POI or JExcel JAR files on your new project's class path. Import the relevant packages into the class that will use them. Start typing, autocomplete will help give you a list of classes and methods that are available.
    If the above is confusing, start over. Go back and re-read how to import external JAR's onto a classpath. Try to use another third-party API to get the hang of it. Then, do it with POI. Follow the examples, even copy-paste a class into your own source tree and try to compile. Gradually, build up your expertise. Now, go to the Javadocs. You should have the ability to use any class or method published in the API.
    - Saish

Maybe you are looking for