Sanady

Uloha 4

Mar 3rd, 2019
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. //MAIN PROGRAM:
  2. /*
  3.  * To change this license header, choose License Headers in Project Properties.
  4.  * To change this template file, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7. package projekt2;
  8.  
  9. import java.util.List;
  10. import javax.persistence.*;
  11.  
  12. /**
  13.  *
  14.  * @author vsa
  15.  */
  16. public class Projekt2 {
  17.  
  18.     /**
  19.      * @param args the command line arguments
  20.      */
  21.     public static void main(String[] args) {
  22.         EntityManagerFactory emf = Persistence.createEntityManagerFactory("Project2PU");
  23.         EntityManager em = emf.createEntityManager();
  24.        
  25.         em.getTransaction().begin();
  26.         Person p = new Person("Jano", 12.0);
  27.         em.getTransaction().commit();
  28.         em.close();
  29.     }
  30.    
  31.     public static void showAllPersons(EntityManager em)
  32.     {
  33.         Query q = em.createNativeQuery("SELECT * FROM T_OSOBA", Person.class);
  34.         List<Person> op = q.getResultList();
  35.        
  36.         for(Person p: op){
  37.             System.out.println("Meno: " +p.getMeno()+"\tNarodenie: "+p.getNarodenie()+"\tTarcha: "+p.getHmotnost()+"\n");
  38.         }
  39.     }
  40.    
  41. }
  42. //=========================================
  43. //PERSON CLASS:
  44. /*
  45.  * To change this license header, choose License Headers in Project Properties.
  46.  * To change this template file, choose Tools | Templates
  47.  * and open the template in the editor.
  48.  */
  49. package projekt2;
  50. import java.util.Date;
  51. import javax.persistence.*;
  52. /**
  53.  *
  54.  * @author vsa
  55.  */
  56. @Entity
  57. @Table(name = "T_OSOBA")
  58. public class Person {
  59.     @GeneratedValue
  60.     @Id
  61.     private Long id;
  62.     @Column(name = "name")
  63.     private String meno;
  64.     @Column(name = "born")
  65.     @Temporal(javax.persistence.TemporalType.DATE)
  66.     private Date narodenie;
  67.     @Column(name = "weight")
  68.     private double hmotnost;
  69.    
  70.  
  71.     public Long getId() {
  72.         return id;
  73.     }
  74.  
  75.     public void setId(Long id) {
  76.         this.id = id;
  77.     }
  78.  
  79.     public String getMeno() {
  80.         return meno;
  81.     }
  82.  
  83.     public Date getNarodenie() {
  84.         return narodenie;
  85.     }
  86.  
  87.     public double getHmotnost() {
  88.         return hmotnost;
  89.     }
  90.  
  91.     public void setMeno(String meno) {
  92.         this.meno = meno;
  93.     }
  94.  
  95.     public void setNarodenie(Date narodenie) {
  96.         this.narodenie = narodenie;
  97.     }
  98.  
  99.     public void setHmotnost(double hmotnost) {
  100.         this.hmotnost = hmotnost;
  101.     }
  102.  
  103.     public Person(){}
  104.    
  105.     public Person(String meno, double hmotnost) {
  106.         this.meno = meno;
  107.         this.hmotnost = hmotnost;
  108.     }
  109.  
  110.    
  111.     @Override
  112.     public String toString() {
  113.         return "Person{" + "id=" + id + ", meno=" + meno + ", narodenie=" + narodenie + ", hmotnost=" + hmotnost + '}';
  114.     }
  115.    
  116. }
  117. //=================================
  118. //Persistence XML
  119. <?xml version="1.0" encoding="UTF-8"?>
  120. <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">
  121.   <persistence-unit name="Project2PU" transaction-type="RESOURCE_LOCAL">
  122.     <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
  123.     <properties>
  124.       <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample"/>
  125.       <property name="javax.persistence.jdbc.user" value="app"/>
  126.       <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
  127.       <property name="javax.persistence.jdbc.password" value="app"/>
  128.       <property name="eclipselink.logging.level" value="FINE"/>
  129.       <exclude-unlisted-classes>false</exclude-unlisted-classes>
  130.     </properties>
  131.   </persistence-unit>
  132. </persistence>
Advertisement
Add Comment
Please, Sign In to add comment