Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MAIN PROGRAM:
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package projekt2;
- import java.util.List;
- import javax.persistence.*;
- /**
- *
- * @author vsa
- */
- public class Projekt2 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- EntityManagerFactory emf = Persistence.createEntityManagerFactory("Project2PU");
- EntityManager em = emf.createEntityManager();
- em.getTransaction().begin();
- Person p = new Person("Jano", 12.0);
- em.getTransaction().commit();
- em.close();
- }
- public static void showAllPersons(EntityManager em)
- {
- Query q = em.createNativeQuery("SELECT * FROM T_OSOBA", Person.class);
- List<Person> op = q.getResultList();
- for(Person p: op){
- System.out.println("Meno: " +p.getMeno()+"\tNarodenie: "+p.getNarodenie()+"\tTarcha: "+p.getHmotnost()+"\n");
- }
- }
- }
- //=========================================
- //PERSON CLASS:
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package projekt2;
- import java.util.Date;
- import javax.persistence.*;
- /**
- *
- * @author vsa
- */
- @Entity
- @Table(name = "T_OSOBA")
- public class Person {
- @GeneratedValue
- @Id
- private Long id;
- @Column(name = "name")
- private String meno;
- @Column(name = "born")
- @Temporal(javax.persistence.TemporalType.DATE)
- private Date narodenie;
- @Column(name = "weight")
- private double hmotnost;
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getMeno() {
- return meno;
- }
- public Date getNarodenie() {
- return narodenie;
- }
- public double getHmotnost() {
- return hmotnost;
- }
- public void setMeno(String meno) {
- this.meno = meno;
- }
- public void setNarodenie(Date narodenie) {
- this.narodenie = narodenie;
- }
- public void setHmotnost(double hmotnost) {
- this.hmotnost = hmotnost;
- }
- public Person(){}
- public Person(String meno, double hmotnost) {
- this.meno = meno;
- this.hmotnost = hmotnost;
- }
- @Override
- public String toString() {
- return "Person{" + "id=" + id + ", meno=" + meno + ", narodenie=" + narodenie + ", hmotnost=" + hmotnost + '}';
- }
- }
- //=================================
- //Persistence XML
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
- <persistence-unit name="Project2PU" transaction-type="RESOURCE_LOCAL">
- <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
- <properties>
- <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample"/>
- <property name="javax.persistence.jdbc.user" value="app"/>
- <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
- <property name="javax.persistence.jdbc.password" value="app"/>
- <property name="eclipselink.logging.level" value="FINE"/>
- <exclude-unlisted-classes>false</exclude-unlisted-classes>
- </properties>
- </persistence-unit>
- </persistence>
Advertisement
Add Comment
Please, Sign In to add comment