Advertisement
Guest User

Untitled

a guest
Feb 13th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.29 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <persistence-unit name="Aplication" transaction-type="RESOURCE_LOCAL">
  4. <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  5. <properties>
  6. <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hibernate?zeroDateTimeBehavior=convertToNull"/>
  7. <property name="javax.persistence.jdbc.user" value="root"/>
  8. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  9. <property name="javax.persistence.jdbc.password" value="verde"/>
  10.  
  11.  
  12. <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
  13. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
  14. <property name="hibernate.hbm2ddl.auto" value="update" />
  15. </properties>
  16. </persistence-unit>
  17. </persistence>
  18.  
  19. package Model;
  20.  
  21. import java.io.Serializable;
  22. import javax.persistence.Column;
  23. import javax.persistence.Entity;
  24. import javax.persistence.GeneratedValue;
  25. import javax.persistence.GenerationType;
  26. import javax.persistence.Id;
  27. import javax.persistence.Table;
  28.  
  29.  
  30. @Entity
  31. @Table
  32. public class Prueba implements Serializable {
  33.  
  34. private static final long serialVersionUID = 1L;
  35. @Id
  36. @GeneratedValue(strategy = GenerationType.AUTO)
  37. private Long id;
  38.  
  39. @Column
  40. private String nombre;
  41.  
  42.  
  43.  
  44. public Long getId() {
  45. return id;
  46. }
  47.  
  48. public void setId(Long id) {
  49. this.id = id;
  50. }
  51.  
  52. public String getNombre() {
  53. return nombre;
  54. }
  55.  
  56. public void setNombre(String nombre) {
  57. this.nombre = nombre;
  58. }
  59.  
  60.  
  61.  
  62. @Override
  63. public int hashCode() {
  64. int hash = 0;
  65. hash += (id != null ? id.hashCode() : 0);
  66. return hash;
  67. }
  68.  
  69. @Override
  70. public boolean equals(Object object) {
  71. // TODO: Warning - this method won't work in the case the id fields are not set
  72. if (!(object instanceof Prueba)) {
  73. return false;
  74. }
  75. Prueba other = (Prueba) object;
  76. if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
  77. return false;
  78. }
  79. return true;
  80. }
  81.  
  82. @Override
  83. public String toString() {
  84. return "Model.Prueba[ id=" + id + " ]";
  85. }
  86.  
  87. }
  88.  
  89. package Model;
  90.  
  91. import Model.exceptions.NonexistentEntityException;
  92. import java.io.Serializable;
  93. import java.util.List;
  94. import javax.persistence.EntityManager;
  95. import javax.persistence.EntityManagerFactory;
  96. import javax.persistence.Query;
  97. import javax.persistence.EntityNotFoundException;
  98. import javax.persistence.criteria.CriteriaQuery;
  99. import javax.persistence.criteria.Root;
  100.  
  101. /**
  102. *
  103. * @author Spencer
  104. */
  105. public class PruebaJpaController implements Serializable {
  106.  
  107. public PruebaJpaController(EntityManagerFactory emf) {
  108. this.emf = emf;
  109. }
  110. private EntityManagerFactory emf = null;
  111.  
  112. public EntityManager getEntityManager() {
  113. return emf.createEntityManager();
  114. }
  115.  
  116. public void create(Prueba prueba) {
  117. EntityManager em = null;
  118. try {
  119. em = getEntityManager();
  120. em.getTransaction().begin();
  121. em.persist(prueba);
  122. em.getTransaction().commit();
  123. } finally {
  124. if (em != null) {
  125. em.close();
  126. }
  127. }
  128. }
  129.  
  130. public void edit(Prueba prueba) throws NonexistentEntityException, Exception {
  131. EntityManager em = null;
  132. try {
  133. em = getEntityManager();
  134. em.getTransaction().begin();
  135. prueba = em.merge(prueba);
  136. em.getTransaction().commit();
  137. } catch (Exception ex) {
  138. String msg = ex.getLocalizedMessage();
  139. if (msg == null || msg.length() == 0) {
  140. Long id = prueba.getId();
  141. if (findPrueba(id) == null) {
  142. throw new NonexistentEntityException("The prueba with id " + id + " no longer exists.");
  143. }
  144. }
  145. throw ex;
  146. } finally {
  147. if (em != null) {
  148. em.close();
  149. }
  150. }
  151. }
  152.  
  153. public void destroy(Long id) throws NonexistentEntityException {
  154. EntityManager em = null;
  155. try {
  156. em = getEntityManager();
  157. em.getTransaction().begin();
  158. Prueba prueba;
  159. try {
  160. prueba = em.getReference(Prueba.class, id);
  161. prueba.getId();
  162. } catch (EntityNotFoundException enfe) {
  163. throw new NonexistentEntityException("The prueba with id " + id + " no longer exists.", enfe);
  164. }
  165. em.remove(prueba);
  166. em.getTransaction().commit();
  167. } finally {
  168. if (em != null) {
  169. em.close();
  170. }
  171. }
  172. }
  173.  
  174. public List<Prueba> findPruebaEntities() {
  175. return findPruebaEntities(true, -1, -1);
  176. }
  177.  
  178. public List<Prueba> findPruebaEntities(int maxResults, int firstResult) {
  179. return findPruebaEntities(false, maxResults, firstResult);
  180. }
  181.  
  182. private List<Prueba> findPruebaEntities(boolean all, int maxResults, int firstResult) {
  183. EntityManager em = getEntityManager();
  184. try {
  185. CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
  186. cq.select(cq.from(Prueba.class));
  187. Query q = em.createQuery(cq);
  188. if (!all) {
  189. q.setMaxResults(maxResults);
  190. q.setFirstResult(firstResult);
  191. }
  192. return q.getResultList();
  193. } finally {
  194. em.close();
  195. }
  196. }
  197.  
  198. public Prueba findPrueba(Long id) {
  199. EntityManager em = getEntityManager();
  200. try {
  201. return em.find(Prueba.class, id);
  202. } finally {
  203. em.close();
  204. }
  205. }
  206.  
  207. public int getPruebaCount() {
  208. EntityManager em = getEntityManager();
  209. try {
  210. CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
  211. Root<Prueba> rt = cq.from(Prueba.class);
  212. cq.select(em.getCriteriaBuilder().count(rt));
  213. Query q = em.createQuery(cq);
  214. return ((Long) q.getSingleResult()).intValue();
  215. } finally {
  216. em.close();
  217. }
  218. }
  219.  
  220. }
  221.  
  222. package Model;
  223.  
  224. import javax.persistence.EntityManagerFactory;
  225. import javax.persistence.Persistence;
  226.  
  227.  
  228. public class PruebaDao {
  229. private static EntityManagerFactory em;
  230. private static PruebaJpaController pjpa;
  231.  
  232. public PruebaDao(){
  233. em = Persistence.createEntityManagerFactory("Aplication");
  234. pjpa = new PruebaJpaController(em);
  235. }
  236.  
  237. public void addPrueba(Prueba pb){
  238. pjpa.create(pb);
  239. }
  240.  
  241. }
  242.  
  243. package javaapplication7;
  244.  
  245. import Model.Prueba;
  246. import Model.PruebaDao;
  247.  
  248. /**
  249. *
  250. * @author Spencer
  251. */
  252. public class JavaApplication7 {
  253.  
  254. /**
  255. * @param args the command line arguments
  256. */
  257. public static void main(String[] args) {
  258. Prueba pb = new Prueba();
  259. PruebaDao pbdao = new PruebaDao();
  260.  
  261. pb.setNombre("numerouno");
  262.  
  263. pbdao.addPrueba(pb);
  264.  
  265. }
  266.  
  267. }
  268.  
  269. feb 13, 2017 4:51:55 AM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
  270. INFO: HHH000204: Processing PersistenceUnitInfo [
  271. name: Aplication
  272. ...]
  273. feb 13, 2017 4:51:56 AM org.hibernate.Version logVersion
  274. INFO: HHH000412: Hibernate Core {5.2.7.Final}
  275. feb 13, 2017 4:51:56 AM org.hibernate.cfg.Environment <clinit>
  276. INFO: HHH000206: hibernate.properties not found
  277. feb 13, 2017 4:51:57 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
  278. INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
  279. feb 13, 2017 4:51:57 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  280. WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
  281. feb 13, 2017 4:51:57 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
  282. INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate?zeroDateTimeBehavior=convertToNull]
  283. feb 13, 2017 4:51:57 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
  284. INFO: HHH10001001: Connection properties: {user=root, password=****}
  285. feb 13, 2017 4:51:57 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
  286. INFO: HHH10001003: Autocommit mode: false
  287. feb 13, 2017 4:51:58 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
  288. INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
  289. feb 13, 2017 4:52:02 AM org.hibernate.dialect.Dialect <init>
  290. INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
  291. feb 13, 2017 4:52:06 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
  292. INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@52a3ff] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement