Guest User

Untitled

a guest
Nov 4th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. package javaapplication11;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Objects;
  5. import javax.persistence.Column;
  6. import javax.persistence.Entity;
  7. import javax.persistence.GeneratedValue;
  8. import javax.persistence.GenerationType;
  9. import javax.persistence.Id;
  10. import javax.persistence.Table;
  11.  
  12. /**
  13. *
  14. * @author kB
  15. */
  16. @Entity
  17. @Table(name="room")
  18. public class NewEntity implements Serializable {
  19.  
  20. private static final long serialVersionUID = 1L;
  21.  
  22. @Id
  23. @GeneratedValue(strategy = GenerationType.AUTO)
  24. @Column(name = "room_id")
  25. private int id;
  26.  
  27. @Column(name = "room_name")
  28. private String name;
  29.  
  30. public NewEntity() {
  31. }
  32.  
  33. public int getId() {
  34. return id;
  35. }
  36.  
  37. public void setId(int id) {
  38. this.id = id;
  39. }
  40.  
  41. public String getName() {
  42. return name;
  43. }
  44.  
  45. public void setName(String name) {
  46. this.name = name;
  47. }
  48.  
  49. @Override
  50. public int hashCode() {
  51. int hash = 5;
  52. hash = 29 * hash + this.id;
  53. hash = 29 * hash + Objects.hashCode(this.name);
  54. return hash;
  55. }
  56.  
  57. @Override
  58. public boolean equals(Object obj) {
  59. if (this == obj) {
  60. return true;
  61. }
  62. if (obj == null) {
  63. return false;
  64. }
  65. if (getClass() != obj.getClass()) {
  66. return false;
  67. }
  68. final NewEntity other = (NewEntity) obj;
  69. if (this.id != other.id) {
  70. return false;
  71. }
  72. if (!Objects.equals(this.name, other.name)) {
  73. return false;
  74. }
  75. return true;
  76. }
  77.  
  78. @Override
  79. public String toString() {
  80. return super.toString(); //To change body of generated methods, choose Tools | Templates.
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. }
  88.  
  89. package javaapplication11;
  90.  
  91. import javax.persistence.EntityManager;
  92. import javax.persistence.EntityManagerFactory;
  93. import javax.persistence.Persistence;
  94.  
  95. /**
  96. *
  97. * @author kB
  98. */
  99. public class JavaApplication11 {
  100.  
  101. /**
  102. * @param args the command line arguments
  103. */
  104. public static void main(String[] args) {
  105. // TODO code application logic here
  106.  
  107. EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaApplication11PU");
  108. EntityManager em = emf.createEntityManager();
  109. em.getTransaction().begin();
  110. NewEntity x = new NewEntity();
  111. x.setName("anan");
  112. em.persist(x);
  113. em.getTransaction().commit();
  114. em.close();
  115. emf.close();
  116. }
  117. }
  118.  
  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="JavaApplication11PU" transaction-type="RESOURCE_LOCAL">
  122. <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
  123. <class>javaapplication11.NewEntity</class>
  124. <properties>
  125. <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/felek"/>
  126. <property name="javax.persistence.jdbc.user" value="root"/>
  127. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  128. <property name="javax.persistence.jdbc.password" value=""/>
  129. <property name="javax.persistence.schema-generation.database.action" value="create"/>
  130. </properties>
  131. </persistence-unit>
  132. </persistence>
Add Comment
Please, Sign In to add comment