Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6539517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:52:05+00:00 2026-05-25T10:52:05+00:00

I design a DAO class this way : public void Class(Class object){ EntityManagerFactory entityManagerFactory

  • 0

I design a DAO class this way :

public void Class(Class object){

    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistUnit");
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    entityManager.getTransaction().begin();
    entityManager.persist(object);
    entityManager.getTransaction().commit();

    entityManagerFactory.close();
    entityManager.close();

}

There’s my persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
    <persistence-unit name="persistUnit" transaction-type="RESOURCE_LOCAL">
        //.. classes
        <properties>
            <property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://<hostname>:3306/<schema>"/>
            <property name="javax.persistence.jdbc.user" value="<username>"/>
            <property name="javax.persistence.jdbc.password" value="<password>"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

There’s some another way to do this kind of operation ? I mean, more sophisticated ?

I’m using Eclipse Indigo and JPA with EclipseLink 2.3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:52:05+00:00Added an answer on May 25, 2026 at 10:52 am

    Well, if the question is how to design a DAO you can find plenty of documentation in difference places like in the Java EE Pattern Catalog.

    Now, I do not think I get your code. Do you really have a method named “Class”? (I do not think that is possible given that “Class” is a reserved word, is it not?)

    Anyway, assuming the name was correct and possible, if that method is part of the public interface of your DAO then you might consider a redesign based on the following ideas:

    You do not initialize the Entity Manager Factory over and over again in method to do just a transaction. This task is quite expensive (in computational terms meaning that it consumes a lot of time and resources). Therefore, you typically initialize your factory once in the lifetime of your application, and use it to create as many entity managers as your application requires.

    An Entity Manager is handled differently depending on the type of application that you are building. For instance Java EE applications can take advantage of container-managed transactions and application context sharing. Assuming your code is that of a JSE application, you do one of two things: a) you can create a single entity manager and share it with all your DAOs, or you can create an entity manager per user-conversation (most typically by transaction) and find a way to inject into your DAOs.

    You might like to take a look at Google Juice, they have a neat way of injecting Entity Manager and transactional support to POJOs using their inversion of control framework.

    You might want to check this other answer on how JPA Entity Manager Contexts works
    Does EntityManager's find() method create new instance of JPA class?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What should be the best way to design a DAO class ? Approach#1: Design
Design class which is generated by C# : // // usepurposeComboBox // this.usepurposeComboBox.DataSource =
Design patterns are usually related to object oriented design. Are there design patterns for
So I've been trying to design a clean way of grabbing data for my
I have a domain object that stores some metadata and some raw bytes. This
Lately I have come across Null Object design pattern and my colleagues say it
I need new design patterns in JPA/ORM instead of POJO/JDBC/DAO patterns. Is there any
Design -- in a perfect world I have one abstract base class A with
This design question needs a bit of context, so please bear with me. I
The design of the UITableView is something like this: I want to make my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.