Advertisement
NelloRizzo

Untitled

May 26th, 2020
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. // TABELLA DELLE CITTA'
  2. package corso.java.model;
  3.  
  4. import javax.persistence.Column;
  5. import javax.persistence.Entity;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.GenerationType;
  8. import javax.persistence.Id;
  9. import javax.persistence.Table;
  10.  
  11. @Entity // promuove la classe ad entità del data layer
  12. @Table(name="cities") // descrive le caratteristiche della tabella che rappresenta la classe
  13. public class City {
  14.     @Id
  15.     @GeneratedValue(strategy = GenerationType.AUTO) // sarà il database a gestire il valore
  16.     private Long id;
  17.    
  18.     @Column // promuove l'attributo della classe in colonna della tabella
  19.     private String name;
  20.     // il campo fiscalCode rappresenta UNA chiave della tabella
  21.     @Column(length = 4)
  22.     private String fiscalCode;
  23.     @Column
  24.     private Long people;
  25.    
  26.     private Province province;
  27.     private Region region;
  28.  
  29.     // Getters
  30.     public String getName() {
  31.         return name;
  32.     }
  33.  
  34.     public String getFiscalCode() {
  35.         return fiscalCode;
  36.     }
  37.  
  38.     public Long getPeople() {
  39.         return people;
  40.     }
  41.  
  42.     public Province getProvince() {
  43.         return province;
  44.     }
  45.  
  46.     public Region getRegion() {
  47.         return region;
  48.     }
  49.  
  50.  
  51.     // Setters
  52.     public void setName(String name) {
  53.         this.name = name;
  54.     }
  55.  
  56.     public void setFiscalCode(String fiscalCode) {
  57.         this.fiscalCode = fiscalCode;
  58.     }
  59.  
  60.     public void setPeople(Long people) {
  61.         this.people = people;
  62.     }
  63.  
  64.     public void setProvince(Province province) {
  65.         this.province = province;
  66.     }
  67.  
  68.     public void setRegion(Region region) {
  69.         this.region = region;
  70.     }
  71.     // Metodi di supporto da Object
  72.     @Override
  73.     public int hashCode() {
  74.         return fiscalCode.hashCode();
  75.     }
  76.  
  77.     @Override
  78.     public boolean equals(Object obj) {
  79.         if (obj instanceof City)
  80.             return hashCode() == obj.hashCode();
  81.         return super.equals(obj);
  82.     }
  83.  
  84.     @Override
  85.     public String toString() {
  86.         return String.format("%s (%s)", name, province.getAcronym());
  87.     }
  88.  
  89.     public Long getId() {
  90.         return id;
  91.     }
  92.  
  93.     public void setId(Long id) {
  94.         this.id = id;
  95.     }
  96.  
  97. }
  98. // TABELLA DELLE PROVINCE
  99. package corso.java.model;
  100.  
  101. import javax.persistence.Column;
  102. import javax.persistence.Entity;
  103. import javax.persistence.GeneratedValue;
  104. import javax.persistence.GenerationType;
  105. import javax.persistence.Id;
  106. import javax.persistence.Table;
  107.  
  108. @Entity
  109. @Table(name = "provinces")
  110. public class Province {
  111.     @Id
  112.     @GeneratedValue(strategy = GenerationType.AUTO) // sarà il database a gestire il valore
  113.     private Long id;
  114.     @Column
  115.     private String acronym;
  116.     @Column
  117.     private String name;
  118.  
  119.     public String getAcronym() {
  120.         return acronym;
  121.     }
  122.  
  123.     public String getName() {
  124.         return name;
  125.     }
  126.  
  127.     public void setAcronym(String acronym) {
  128.         this.acronym = acronym;
  129.     }
  130.  
  131.     public void setName(String name) {
  132.         this.name = name;
  133.     }
  134.  
  135.     public Long getId() {
  136.         return id;
  137.     }
  138.  
  139.     public void setId(Long id) {
  140.         this.id = id;
  141.     }
  142. }
  143. // TABELLA DELLE REGIONI
  144. package corso.java.model;
  145.  
  146. import javax.persistence.Column;
  147. import javax.persistence.Entity;
  148. import javax.persistence.GeneratedValue;
  149. import javax.persistence.GenerationType;
  150. import javax.persistence.Id;
  151. import javax.persistence.Table;
  152.  
  153. @Entity
  154. @Table(name = "regions")
  155. public class Region {
  156.     @Id
  157.     @GeneratedValue(strategy = GenerationType.AUTO) // sarà il database a gestire il valore
  158.     private Long id;
  159.     @Column
  160.     private String name;
  161.  
  162.     public String getName() {
  163.         return name;
  164.     }
  165.  
  166.     public void setName(String name) {
  167.         this.name = name;
  168.     }
  169.  
  170.     public Long getId() {
  171.         return id;
  172.     }
  173.  
  174.     public void setId(Long id) {
  175.         this.id = id;
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement