Sanady

Vypis mena

Mar 3rd, 2019
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. //MAIN
  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 cviko4;
  8.  
  9. import java.util.List;
  10. import javax.persistence.*;
  11. /**
  12.  *
  13.  * @author vsa
  14.  */
  15. public class Cviko4 {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args) {
  21.         EntityManagerFactory emf = Persistence.createEntityManagerFactory("cviko4PU");
  22.         EntityManager em = emf.createEntityManager();
  23.        
  24.         Query q = em.createNativeQuery("SELECT * FROM Osoba", Osoba.class);
  25.         List<Osoba> zo = q.getResultList();
  26.        
  27.         em.getTransaction().begin();
  28.        
  29.         for(Osoba o: zo){
  30.             System.out.println("Name: "+o.getMeno());
  31.         }
  32.        
  33.         em.getTransaction().commit();
  34.        
  35.         em.close();
  36.     }
  37. }
  38. //=========================
  39. //OSOBA:
  40. /*
  41.  * To change this license header, choose License Headers in Project Properties.
  42.  * To change this template file, choose Tools | Templates
  43.  * and open the template in the editor.
  44.  */
  45. package cviko4;
  46. import java.util.Date;
  47. import javax.persistence.*;
  48. /**
  49.  *
  50.  * @author vsa
  51.  */
  52. @Entity
  53. public class Osoba {
  54.     @Id
  55.     private String meno;
  56.     @Temporal(javax.persistence.TemporalType.DATE)
  57.     private Date narodenie;
  58.     private double vaha;
  59.  
  60.     public String getMeno() {
  61.         return meno;
  62.     }
  63.  
  64.     public double getVaha() {
  65.         return vaha;
  66.     }
  67.  
  68.     public void setMeno(String meno) {
  69.         this.meno = meno;
  70.     }
  71.  
  72.     public void setVaha(double vaha) {
  73.         this.vaha = vaha;
  74.     }
  75.    
  76.     public Osoba(){}
  77.    
  78.     public Osoba(String meno, double vaha) {
  79.         this.meno = meno;
  80.         this.vaha = vaha;
  81.     }
  82.    
  83.    
  84. }
  85. //=======================
  86. //XML:
  87. <?xml version="1.0" encoding="UTF-8"?>
  88. <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">
  89.   <persistence-unit name="cviko4PU" transaction-type="RESOURCE_LOCAL">
  90.     <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
  91.     <properties>
  92.       <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample"/>
  93.       <property name="javax.persistence.jdbc.user" value="app"/>
  94.       <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
  95.       <property name="javax.persistence.jdbc.password" value="app"/>
  96.       <property name="eclipselink.logging.level" value="FINE"/>
  97.       <exclude-unlisted-classes>false</exclude-unlisted-classes>
  98.     </properties>
  99.   </persistence-unit>
  100. </persistence>
Advertisement
Add Comment
Please, Sign In to add comment