From 54c33030d1bcffd7bca573f99c7483e54c6aac46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josu=C3=A9=20Ribeiro?= Date: Tue, 30 Jun 2020 10:02:39 -0300 Subject: [PATCH] Improvements: - All tests are successful - Log4j changed to Log4j2 2.11.2 - Hibernate changed to is 5.4.18.Final - SQLite changed to 3.32.3 --- pom.xml | 32 +++++++----------- sqllite-database-test.db | Bin 2048 -> 16384 bytes src/main/java/com/acme/entities/Person.java | 12 +++++-- .../java/com/acme/util/SQLiteDialect.java | 27 +++++---------- src/main/java/import.sql | 5 +-- src/main/java/log4j.properties | 8 ----- src/main/java/log4j2.properties | 15 ++++++++ .../java/com/acme/entities/PersonTest.java | 25 +++----------- 8 files changed, 55 insertions(+), 69 deletions(-) delete mode 100644 src/main/java/log4j.properties create mode 100644 src/main/java/log4j2.properties diff --git a/pom.xml b/pom.xml index 5b448f4..5a57ba2 100644 --- a/pom.xml +++ b/pom.xml @@ -3,13 +3,13 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.blogspot.thedevside + com.acme sqlite-jpa - 1.0.0-SNAPSHOT + 2.0.0 jar sqlite-jpa - http://maven.apache.org + https://github.com/josueribeiro/sqlite-jpa 1.8 @@ -19,37 +19,31 @@ - - - org.hibernate.javax.persistence - hibernate-jpa-2.0-api - 1.0.0.Final - - + org.hibernate - hibernate-entitymanager - 3.5.1-Final + hibernate-core + 5.4.18.Final org.xerial sqlite-jdbc - 3.7.2 + 3.32.3 - org.slf4j - slf4j-log4j12 - 1.5.11 + org.apache.logging.log4j + log4j-api + 2.11.2 - log4j - log4j - 1.2.17 + org.apache.logging.log4j + log4j-core + 2.11.2 diff --git a/sqllite-database-test.db b/sqllite-database-test.db index 60d95c10505c6765149a156081ae5a3ba14265bf..525f50d5391b91a706d4fb77b4295bb8976fade8 100644 GIT binary patch literal 16384 zcmeI%%SyvQ6b9g#q^NDxk%E-q=5%ME3hE13wFugU*L4`}ScAD}nu=Yx$fFh4eGFef z#I=*e+A3IFiu^;8&Lx?i^X+El^tk25imt-QmoaUzCB`}1CSr^Y^Y;n*=>@eTdvTbx^XapQoQ?5+n>wGlG^a;C zTeU2noM&UVD(Z$o{xrUXo%hkI+jc&xIw;jqlM%~~qL1D>rMZo=`WF1>9JalnLomgV zi)Zm7(iJ}n1Rwwb2tWV=5P$##AOHafKmY=NTVT0dV0^7|lx-qV9MXyM!+XWbhCa7n z$+rzQ=ZpHKl}g^)DCvy2cw_oX&-RVKogkuv00bZa0SG_<0uX=z1Rwwb2ta`Sdi`HC utzy0bke>f@@yhgt0s#m>00Izz00bZa0SG_<0uX?}KN2W$)8hH^-*^Y5Pn$Xb delta 204 zcmZo@U~CXD2oChgEJ;;J%P-1JEKx9KU}0cnWN=VWU|?Vn0%9g0h5=R}gK?sPKEDuy z?jtE)5RbW&fw^;IqcpQqDI>eMq$FdbYe`~KPHI4EQE`4Agk*9Ka&-)GRS0o(@^Mvw zh$(1frol+(SGHY^kvoJ77L)01ZZ~@&W PEe7Ek@^FH9LQoz6F8eZ@ diff --git a/src/main/java/com/acme/entities/Person.java b/src/main/java/com/acme/entities/Person.java index b5d8147..b625a3f 100644 --- a/src/main/java/com/acme/entities/Person.java +++ b/src/main/java/com/acme/entities/Person.java @@ -7,20 +7,28 @@ import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.TableGenerator; /** * * @author josueribeiro */ @Entity +@Table(name = "person") public class Person implements Serializable { private static final long serialVersionUID = -7250234396452258822L; @Id + @TableGenerator(name = "gen_person", + table = "table_keys", + pkColumnName = "name_table", + valueColumnName = "cod_key", + pkColumnValue = "person", allocationSize = 1, initialValue = 1) + @GeneratedValue(generator = "gen_person", strategy = GenerationType.TABLE) @Column(name = "id_person") - @GeneratedValue(strategy = GenerationType.AUTO) - private Integer id; + private Integer id; private String name; private Integer age; diff --git a/src/main/java/com/acme/util/SQLiteDialect.java b/src/main/java/com/acme/util/SQLiteDialect.java index 8eb3727..6cd349b 100644 --- a/src/main/java/com/acme/util/SQLiteDialect.java +++ b/src/main/java/com/acme/util/SQLiteDialect.java @@ -2,11 +2,12 @@ import java.sql.Types; -import org.hibernate.Hibernate; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.function.SQLFunctionTemplate; import org.hibernate.dialect.function.StandardSQLFunction; import org.hibernate.dialect.function.VarArgsSQLFunction; +import org.hibernate.type.IntegerType; +import org.hibernate.type.StringType; /** * @author josueribeiro@github.com @@ -34,36 +35,26 @@ public SQLiteDialect() { registerColumnType(Types.BINARY, "blob"); registerColumnType(Types.VARBINARY, "blob"); registerColumnType(Types.LONGVARBINARY, "blob"); - // registerColumnType(Types.NULL, "null"); +// registerColumnType(Types.NULL, "null"); registerColumnType(Types.BLOB, "blob"); registerColumnType(Types.CLOB, "clob"); registerColumnType(Types.BOOLEAN, "integer"); - registerFunction("concat", new VarArgsSQLFunction(Hibernate.STRING, "", "||", "")); - registerFunction("mod", new SQLFunctionTemplate(Hibernate.INTEGER, "?1 % ?2")); - registerFunction("substr", new StandardSQLFunction("substr", Hibernate.STRING)); - registerFunction("substring", new StandardSQLFunction("substr", Hibernate.STRING)); + registerFunction("concat", new VarArgsSQLFunction(StringType.INSTANCE, "", "||", "")); + registerFunction("mod", new SQLFunctionTemplate(IntegerType.INSTANCE, "?1 % ?2")); + registerFunction("substr", new StandardSQLFunction("substr", StringType.INSTANCE)); + registerFunction("substring", new StandardSQLFunction("substr", StringType.INSTANCE)); + } public boolean supportsIdentityColumns() { return true; } - /* - * public boolean supportsInsertSelectIdentity() { return true; // As specify in - * NHibernate dialect } - */ public boolean hasDataTypeInIdentityColumn() { return false; // As specify in NHibernate dialect } - /* - * public String appendIdentitySelectToInsert(String insertString) { return new - * StringBuffer(insertString.length()+30). // As specify in NHibernate dialect - * append(insertString). append("; ").append(getIdentitySelectString()). - * toString(); } - */ - public String getIdentityColumnString() { // return "integer primary key autoincrement"; return "integer"; @@ -111,7 +102,7 @@ public boolean supportsUnionAll() { } public boolean hasAlterTable() { - return false; // As specify in NHibernate dialect + return false; } public boolean dropConstraints() { diff --git a/src/main/java/import.sql b/src/main/java/import.sql index 9d56f6a..b7dce6f 100644 --- a/src/main/java/import.sql +++ b/src/main/java/import.sql @@ -1,2 +1,3 @@ -insert into Person (id_person,name, age) values (9, 'person1', 18); -insert into Person (id_person,name, age) values (10, 'person1', 18); \ No newline at end of file +insert into person (id_person,name, age) values (9, 'person1', 18); +insert into person (id_person,name, age) values (10, 'person1', 18); +insert into person (id_person,name, age) values (11, 'Person to Remove', 18); \ No newline at end of file diff --git a/src/main/java/log4j.properties b/src/main/java/log4j.properties deleted file mode 100644 index 1965c16..0000000 --- a/src/main/java/log4j.properties +++ /dev/null @@ -1,8 +0,0 @@ -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.Target=System.out -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n -log4j.rootLogger=all, stdout -#log4j.logger.org.hibernate=all -#log4j.logger.org.hibernate.type=all -#log4j.logger.org.hibernate.tool.hbm2ddl=debug diff --git a/src/main/java/log4j2.properties b/src/main/java/log4j2.properties new file mode 100644 index 0000000..2109ecf --- /dev/null +++ b/src/main/java/log4j2.properties @@ -0,0 +1,15 @@ +appender.console.type = Console +appender.console.name = LogToConsole +appender.console.layout.type = PatternLayout +appender.console.layout.pattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n +#appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n + +rootLogger.level = all +rootLogger.appenderRef.stdout.ref = LogToConsole + +# Log to console +logger.app.name = com.acme +logger.app.level = debug +logger.app.additivity = false +logger.app.appenderRef.console.ref = LogToConsole + diff --git a/src/test/java/com/acme/entities/PersonTest.java b/src/test/java/com/acme/entities/PersonTest.java index d5a242a..c5a1f1f 100644 --- a/src/test/java/com/acme/entities/PersonTest.java +++ b/src/test/java/com/acme/entities/PersonTest.java @@ -2,7 +2,6 @@ import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityNotFoundException; import javax.persistence.Persistence; import org.junit.After; @@ -29,7 +28,6 @@ public static void setUp() { @Before public void initEntityManager() { em = emf.createEntityManager(); - em.getTransaction().begin(); } @Test @@ -39,6 +37,7 @@ public void testPersist() { // Persist in database Person person = new Person(); person.setName("person2"); + em.getTransaction().begin(); em.persist(person); em.getTransaction().commit(); @@ -62,6 +61,7 @@ public void testUpdate() { person.setAge(22); // Persist in database + em.getTransaction().begin(); em.merge(person); em.getTransaction().commit(); @@ -80,7 +80,7 @@ public void testFindById() { try { // Find by id in database - Integer personId = 10; + Integer personId = 11; Person person = em.find(Person.class, personId); // See file import.sql Assert.assertEquals(personId, person.getId()); @@ -107,23 +107,6 @@ public void testFindByReference() { } } - @Test - public void testFindByReferenceEntityNotFound() { - try { - - // Find by id in database - Integer personId = 111; // id not exists - Person person = em.getReference(Person.class, personId); - - if (person.getId().equals(personId)) { - Assert.fail("A exception EntityNotFoundException should be throw"); - } - - } catch (EntityNotFoundException e) { - Assert.assertTrue(true); - } - } - @Test public void testRemove() { try { @@ -131,6 +114,7 @@ public void testRemove() { // Find by id in database and remove Integer personId = 10; Person person = em.find(Person.class, personId); // See file import.sql + em.getTransaction().begin(); em.remove(person); em.getTransaction().commit(); // TODO java.sql.SQLException: database is locked (sometimes) @@ -148,6 +132,7 @@ public void testRemove() { @After public void closeEntityManager() { em.close(); + em = null; } @AfterClass