Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. CREATE TABLE unit
  2. (
  3.   id BIGINT PRIMARY KEY AUTO_INCREMENT,
  4.   code VARCHAR(20) UNIQUE NOT NULL,
  5.   description VARCHAR(100) NOT NULL
  6. );
  7.  
  8. CREATE TABLE building
  9. (
  10.   id BIGINT PRIMARY KEY AUTO_INCREMENT,
  11.   code VARCHAR(20) UNIQUE NOT NULL,
  12.   name VARCHAR(100) NOT NULL,
  13.   latitude  NUMERIC(8,6),
  14.   longitude NUMERIC(8,6)
  15. );
  16.  
  17. CREATE TABLE link_unit_building
  18. (
  19.   id BIGINT PRIMARY KEY AUTO_INCREMENT,
  20.   unit_id BIGINT NOT NULL,
  21.   building_id BIGINT NOT NULL
  22. );
  23.  
  24. ALTER TABLE link_unit_building ADD(
  25.  CONSTRAINT building_fk
  26.  FOREIGN KEY (building_id)
  27.  REFERENCES building(id)
  28.   ON DELETE CASCADE ON UPDATE CASCADE,
  29.  CONSTRAINT unit_fk
  30.  FOREIGN KEY (unit_id)
  31.  REFERENCES unit (id)
  32.  ON DELETE CASCADE ON UPDATE CASCADE);
  33.  
  34.  
  35. ----------------------------------------------------------------------------------------------------------------------------------
  36. package zesp03.entity;
  37.  
  38. import javax.persistence.*;
  39.  
  40. /**
  41.  * Created by Berent on 2017-02-20.
  42.  */
  43.  
  44. @Entity
  45. @Table(name = "unit")
  46. public class Unit {
  47.  
  48.     @Id
  49.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  50.     private Long id;
  51.  
  52.     @Column(length = 20, unique = true, nullable = false)
  53.     private String code;
  54.  
  55.     @Column(length = 100, nullable = false)
  56.     private String description;
  57.  
  58.  
  59. }
  60.  
  61. ----------------------------------------------------------------------------------------------------------------------------------
  62.  
  63. package zesp03.entity;
  64. import javax.persistence.*;
  65.  
  66. /**
  67.  * Created by Berent on 2017-02-20.
  68.  */
  69.  
  70. @Entity
  71. @Table(name = "building")
  72. public class Building {
  73.     @Id
  74.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  75.     private Long id;
  76.  
  77.     @Column(length = 20, unique = true, nullable = false)
  78.     private String code;
  79.  
  80.     @Column(length = 100, nullable = false)
  81.     private String name;
  82.  
  83.     @Column(precision=8, scale=6)
  84.     private Number latitude;
  85.  
  86.     @Column(precision=8, scale=6)
  87.     private Number longitude;
  88.  
  89.  
  90. }
  91.  
  92. ---------------------------------------------------------------------------------------------------------------------------
  93. package zesp03.entity;
  94.  
  95. import javax.persistence.*;
  96.  
  97. /**
  98.  * Created by Berent on 2017-02-20.
  99.  */
  100.  
  101. @Entity
  102. @Table(name = "link_unit_building")
  103. public class Link_unit_building {
  104.     @Id
  105.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  106.     private Long id;
  107.  
  108.     @Column(nullable = false)
  109.     private Long unit_id;
  110.  
  111.     @Column(nullable = false)
  112.     private Long building_id;
  113.  
  114.     @OneToOne(fetch = FetchType.LAZY)
  115.     @JoinColumn(
  116.             name = "building_id",
  117.             foreignKey = @ForeignKey(name = "building_fk"),
  118.             nullable = false,
  119.             unique = true
  120.     )
  121.     private Building building;
  122.  
  123.     @OneToOne(fetch = FetchType.LAZY)
  124.     @JoinColumn(
  125.             name = "unit_id",
  126.             foreignKey = @ForeignKey(name = "unit_fk"),
  127.             nullable = false,
  128.             unique = true
  129.     )
  130.     private Unit unit;
  131.  
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement