Reputation: 195
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?
Upvotes: 0
Views: 3582
Reputation: 22710
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.
Advantages of JDBC
Disadvantages of JDBC
Benefits of ORM
Disadvantages of ORM
Upvotes: 0
Reputation: 792
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
Upvotes: 1