<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.st</groupId> <artifactId>jpaoracle</artifactId> <version>0.0.1-SNAPSHOT</version> <name>jpaoracle</name> <dependencies> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc10</artifactId> <version>19.21.0.0</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.6.0.Final</version> </dependency> </dependencies> <properties> <maven.compiler.source>20</maven.compiler.source> <maven.compiler.target>20</maven.compiler.target> </properties> </project>
<?xml version="1.0" encoding="UTF-8"?> <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_2_0.xsd" version="2.0"> <persistence-unit name="oracle" transaction-type="RESOURCE_LOCAL"> <!-- Persistence provider --> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <!-- domain classes --> <class>entities.Course</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" /> <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521/xepdb1" /> <property name="javax.persistence.jdbc.user" value="demo" /> <property name="javax.persistence.jdbc.password" value="demo" /> <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle12cDialect" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> </properties> </persistence-unit> </persistence>
package entities; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "courses") public class Course { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(length = 20, nullable = false) private String title; @Column(nullable = false) private int fee; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getFee() { return fee; } public void setFee(int fee) { this.fee = fee; } }
import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import entities.Course; public class AddCourse { public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("oracle"); EntityManager em = emf.createEntityManager(); Course c = new Course(); c.setTitle("JPA using Hibernate"); c.setFee(10000); em.getTransaction().begin(); // must begin a transaction em.persist(c); // will eventually results in INSERT command em.getTransaction().commit(); // commit transaction em.close(); } }
create table courses ( id number(10,0) generated as identity, fee number(10,0) not null, title varchar2(20 char) not null, primary key (id) )
insert into courses (id, fee, title) values (default, ?, ?)
import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import entities.Course; public class ListCourses { public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("oracle"); EntityManager em = emf.createEntityManager(); var query = em.createQuery("from Course", Course.class); // JPQL var courses = query.getResultList(); for (var c : courses) { System.out.printf("%-20s %d\n", c.getTitle(), c.getFee()); } em.close(); emf.close(); } }
select course0_.id as id1_0_, course0_.fee as fee2_0_, course0_.title as title3_0_ from courses course0_