From: Kai Moritz Date: Sun, 13 Nov 2016 14:56:56 +0000 (+0100) Subject: Fixed the akquisition of a database-connection, if one is needed X-Git-Tag: hibernate-maven-plugin-2.1.0~9 X-Git-Url: http://juplo.de/gitweb/?p=hibernate4-maven-plugin;a=commitdiff_plain;h=653ab8054c9232471c3c1192d5074010f378a067 Fixed the akquisition of a database-connection, if one is needed * The helper-class SimpleConnectionProvider was not changed to reflect the alterd handling of the configuration parameters. * If only the user is missing, SimpleConnectionProvider tries to akquire a connection, because for example H2 does not always need a user, hence this is an sufficient configuration. * If an exception is thrown during database akquisition, it is only logged, instead of rethrown. * Adapted the example-code for the hibernate-bug HHH-9615 to function as an additional integration-test and used it, to proof, that an database connection can be akquired, if only the driver and the URL is present. --- diff --git a/src/it/hibernate-lob-converter-bug/create.sql b/src/it/hibernate-lob-converter-bug/create.sql new file mode 100644 index 00000000..ed04fc4c --- /dev/null +++ b/src/it/hibernate-lob-converter-bug/create.sql @@ -0,0 +1,7 @@ +create sequence hibernate_sequence start with 1 increment by 1; + + create table MyEntity ( + id integer not null, + status blob, + primary key (id) + ); diff --git a/src/it/hibernate-lob-converter-bug/pom.xml b/src/it/hibernate-lob-converter-bug/pom.xml index 3d1eefd8..c98a1cb5 100644 --- a/src/it/hibernate-lob-converter-bug/pom.xml +++ b/src/it/hibernate-lob-converter-bug/pom.xml @@ -24,19 +24,11 @@ h2 1.3.168 - - - junit - junit - 4.11 - test - org.hibernate hibernate-entitymanager 5.1.0.Final - @@ -53,6 +45,24 @@ true + + de.juplo + hibernate-maven-plugin + @project.version@ + + false + true + true + + + + create + + create + + + + diff --git a/src/it/hibernate-lob-converter-bug/src/main/java/io/blep/MyEntity.java b/src/it/hibernate-lob-converter-bug/src/main/java/io/blep/MyEntity.java new file mode 100644 index 00000000..e8bbd36a --- /dev/null +++ b/src/it/hibernate-lob-converter-bug/src/main/java/io/blep/MyEntity.java @@ -0,0 +1,48 @@ +package io.blep; + +import javax.persistence.*; + +/** + * @author blep + */ +@Entity +public class MyEntity { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Integer id; + + @Convert(converter = MyConverter.class) + @Lob + private String status; + + @Converter + public static class MyConverter implements AttributeConverter { + + @Override + public Integer convertToDatabaseColumn(String attribute) { + return attribute == null ? 0 : attribute.length(); + } + + @Override + public String convertToEntityAttribute(Integer dbData) { + return ""; + } + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + +} diff --git a/src/it/hibernate-lob-converter-bug/src/main/resources/META-INF/persistence.xml b/src/it/hibernate-lob-converter-bug/src/main/resources/META-INF/persistence.xml new file mode 100644 index 00000000..78e416df --- /dev/null +++ b/src/it/hibernate-lob-converter-bug/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,14 @@ + + + + + org.hibernate.jpa.HibernatePersistenceProvider + io.blep.MyEntity + + + + + + + + diff --git a/src/it/hibernate-lob-converter-bug/src/test/java/io/blep/LobConverterTest.java b/src/it/hibernate-lob-converter-bug/src/test/java/io/blep/LobConverterTest.java deleted file mode 100644 index b08b8584..00000000 --- a/src/it/hibernate-lob-converter-bug/src/test/java/io/blep/LobConverterTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package io.blep; - -import org.junit.Test; - -import javax.persistence.*; - -/** - * @author blep - */ -public class LobConverterTest { - - @Test - public void testName() throws Exception { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("samplePU"); - - final EntityManager em = emf.createEntityManager(); - - em.getTransaction().begin(); - em.persist(new MyEntity()); - em.getTransaction().commit(); - } -} diff --git a/src/it/hibernate-lob-converter-bug/src/test/java/io/blep/MyEntity.java b/src/it/hibernate-lob-converter-bug/src/test/java/io/blep/MyEntity.java deleted file mode 100644 index e8bbd36a..00000000 --- a/src/it/hibernate-lob-converter-bug/src/test/java/io/blep/MyEntity.java +++ /dev/null @@ -1,48 +0,0 @@ -package io.blep; - -import javax.persistence.*; - -/** - * @author blep - */ -@Entity -public class MyEntity { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Integer id; - - @Convert(converter = MyConverter.class) - @Lob - private String status; - - @Converter - public static class MyConverter implements AttributeConverter { - - @Override - public Integer convertToDatabaseColumn(String attribute) { - return attribute == null ? 0 : attribute.length(); - } - - @Override - public String convertToEntityAttribute(Integer dbData) { - return ""; - } - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - -} diff --git a/src/it/hibernate-lob-converter-bug/src/test/resources/META-INF/persistence.xml b/src/it/hibernate-lob-converter-bug/src/test/resources/META-INF/persistence.xml deleted file mode 100644 index 78e416df..00000000 --- a/src/it/hibernate-lob-converter-bug/src/test/resources/META-INF/persistence.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - org.hibernate.jpa.HibernatePersistenceProvider - io.blep.MyEntity - - - - - - - - diff --git a/src/it/hibernate-lob-converter-bug/verify.bsh b/src/it/hibernate-lob-converter-bug/verify.bsh new file mode 100644 index 00000000..ce2e5a56 --- /dev/null +++ b/src/it/hibernate-lob-converter-bug/verify.bsh @@ -0,0 +1,7 @@ +import de.juplo.test.FileComparator; + + +FileComparator comparator = new FileComparator(basedir); + +if (!comparator.isEqual("create.sql","target/create.sql")) + return false; diff --git a/src/main/java/de/juplo/plugins/hibernate/SimpleConnectionProvider.java b/src/main/java/de/juplo/plugins/hibernate/SimpleConnectionProvider.java index 36fe5614..1983b1ff 100644 --- a/src/main/java/de/juplo/plugins/hibernate/SimpleConnectionProvider.java +++ b/src/main/java/de/juplo/plugins/hibernate/SimpleConnectionProvider.java @@ -45,33 +45,16 @@ class SimpleConnectionProvider implements ConnectionProvider MojoFailureException { - String driver = (String) - (properties.containsKey(DRIVER) - ? properties.getProperty(DRIVER) - : properties.getProperty(JDBC_DRIVER) - ); - String url = (String) - (properties.containsKey(URL) - ? properties.getProperty(URL) - : properties.getProperty(JDBC_URL) - ); - String user = (String) - (properties.containsKey(USER) - ? properties.getProperty(USER) - : properties.getProperty(JDBC_USER) - ); - String password = (String) - (properties.containsKey(PASS) - ? properties.getProperty(PASS) - : properties.getProperty(JDBC_PASSWORD) - ); - - if (driver == null || url == null || user == null) + String driver = (String)properties.getProperty(DRIVER); + String url = (String)properties.getProperty(URL); + String user = (String)properties.getProperty(USER); + String password = (String)properties.getProperty(PASS); + + if (driver == null || url == null) { log.info("No connection opened, because connection information is incomplete"); log.info("Driver-Class: " + driver); log.info("URL: " + url); - log.info("User: " + user); return; } @@ -84,16 +67,16 @@ class SimpleConnectionProvider implements ConnectionProvider .registerDriver(new DriverProxy((Driver) driverClass.newInstance())); log.debug( - "Opening JDBC-connection to " + properties.getProperty(URL) + - " as " + properties.getProperty(USERNAME) + - " with password " + properties.getProperty(PASSWORD) + "Opening JDBC-connection to " + url + + " as " + user + + " with password " + password ); connection = DriverManager.getConnection(url, user, password); } catch (Exception e) { - throw new MojoFailureException("Could not open the JDBC-connection", e); + log.info("Could not open the JDBC-connection: " + e.getMessage()); } }