简体   繁体   中英

How to add JPA to existing eclipse project?

I have an existing eclipse project (JAVA application), now I created a table in xampp - phpMyAdmin. In the project I need to implement all the queries and database in two ways:

1) JDBC

2) JPA

and to choose one of them using the Spring mechanism.

So I have some questions:

1) How to add the JPA to the existing project? because I read that I need to
have a'persistence.xml' file which need to be located at 'META-INF' folder which I don't have in the java project.

2) How to connect to my database in the phpMyAdmin using the JPA?

3) How to choose between the JDBC and JPA using the Spring mechanism?

Answering a part of the question, if you're using Hibernate as JPA Provider, you can change from JPA to JDBC this way:

Session session = em.unwrap(Session.class);
session.doWork(new Work() {

    @Override
    public void execute(Connection connection) throws SQLException {
        Statement stmt = connection.createStatement();
        String nativeSql = "SELECT * FROM USERS"; 
        stmt.executeUpdate(nativeSql);
        stmt.close();
    }
});

Answering questions 1 and 2: You just need to create, as you said, a file named persistence.xml, like this:

<?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="MyJPA" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydb"/>
            <property name="javax.persistence.jdbc.user" value="user"/>
            <property name="javax.persistence.jdbc.password" value="pass"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
        </properties>
    </persistence-unit>
</persistence>

Now, about the way Spring works with JPA, I can't answer you, maybe someone else. But I hope this helps

1) How to add the JPA to the existing project? because I read that I need to have a'persistence.xml' file which need to be located at 'META-INF' folder which I don't have in the java project.

You can add it manually.

Sample

<persistence 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"
                 version="1.0">
         <persistence-unit name="foo" transaction-type="RESOURCE_LOCAL">
            <class>org.bar.foobar</class>
                // Add required classes here
         </persistence-unit>

        // You can add more persistence unit as per required.
 </persistence>

2) How to connect to my database in the phpMyAdmin using the JPA?

phpMyAdmin is just another tool to work with MySQL and you should bother about database and not about tool. Check this link , you might find this helpful

3) How to choose between the JDBC and JPA using the Spring mechanism?

JDBC is the lowest level and others are built on top of it. I prefer JDBC when it to comes to performance.Also when I just want to edit a particular column on very frequent basis I prefer JDBC over JPA.

JDBC VS ORM (JPA)

Advantages of JDBC

  • Clean and easily for small programs
  • JDBC provides good performance with large amount of data
  • Small JDBC programs can be developed very easily
  • Very good for small applications

Disadvantages of JDBC

  • JDBC is not easily if it is used in large projects. There is a big programming overhead.
  • Programmer must hardcode the Transactions and concurrency code in the application.
  • Handling the JDBC connections and properly closing the connection is also a big issue. Properly closing the connection is must.
  • JDBC is not good for big applications

Benefits of ORM

  • No need to deal with the SQL Queries to save and retrieve the data
  • Simple configuration
  • Standardized API to persist the business objects
  • Fast development of application
  • Concurrency support
  • Excellent cashing support for better performance of the application
  • Injected transaction management
  • Configurable logging
  • Easy to learn and use

Disadvantages of ORM

  • Slow performance in case of large batch updates
  • Little slower than JDBC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM