Is it possible to generate artifacts for all the tables at once with MyBatis Generator? - automation

I am using MyBatis Generator 1.3.1 from the command-line. I read in the documentation that I need to specify at least one table for object generation, but I was hoping maybe it is possible to use some wildcard and have mappers for all the tables generated at once? We don't want to use Hibernate, because MyBatis seems to handle custom types in the database better.
Thank you for your help!

You can use SQL wildcards, ie:
<table tableName="%">
<property name="useActualColumnNames" value="true"/>
</table>

You can reference: link
If you use MySQL, The crucial point is this:
<table schema="dbName" tableName="%"</table> and
<property name="nullCatalogMeansCurrent" value="true" />
reference:http://www.mybatis.org/generator/usage/intro.html
Settings like this:
<generatorConfiguration>
<properties resource="mybatis-generator/generator.properties"></properties>
<classPathEntry location="${driverLocation}"/>
<context id="default" targetRuntime="MyBatis3">
<property name="javaFileEncoding" value="UTF-8"/>
<commentGenerator type="org.zhang.generator.MyCommentGenerator">
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection
driverClass="${driverClassName}"
connectionURL="${url}"
userId="${username}"
password="${password}">
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="true"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="daoMappers" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table schema="dbName" tableName="%"
enableSelectByPrimaryKey="true"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
enableDeleteByPrimaryKey="false"
enableUpdateByPrimaryKey="false"
enableInsert="false">
<property name="useActualColumnNames" value="true"></property>
</table>
</generatorConfiguration>

Related

Is the Eclipselink connection pool meant to be used in production?

I am currently trying different connection pools together with eclipselink. The one I am currently configuring is the one by eclipselink itself. Now I am not sure if it is meant to be used in production or if I should use another such as HikariCP? Also I am not sure how to detect if the Eclipselink connection pool is really running. My configuration is the following:
persistence.xml
...
<!-- Configuring Eclipse Link -->
<property name="javax.persistence.jdbc.driver" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:jtds:sqlserver://SERVER/dev;instance=MSSQL2008R2"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="eclipselink.persistence-context.flush-mode" value="commit" /> <!-- Flushing occurs at transaction commit. (https://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/p_persistence_context_flushmode.htm)-->
<!-- We use the EclipseLink internal components (https://www.eclipse.org/eclipselink/documentation/2.6/jpa/extensions/persistenceproperties_ref.htm)-->
<!-- Eclipselink jdbc configuration -->
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/> <!-- Batch-writing is supported by mssql and our jdbc driver (https://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/p_jdbc_batchwriting.htm) -->
<!-- <property name="eclipselink.jdbc.batch-writing.size" value="10000"/> --> <!--Performance testing http://java-persistence-performance.blogspot.ch/2013/05/batch-writing-and-dynamic-vs.html -->
<property name="eclipselink.jdbc.bind-parameters" value="true"/> <!-- Parameterized sql queries -->
<property name="eclipselink.jdbc.cache-statements" value="true"/> <!-- https://docs.oracle.com/middleware/1212/core/ASPER/toplink.htm#ASPER99838 -->
<property name="eclipselink.jdbc.cache-statements.size" value="10000"/>
<property name="eclipselink.jdbc.allow-native-sql-queries" value="false"/> <!-- We explicitly set this, even though it is disabled by multitenancy by default -->
<!-- Eclipselink connection pool configuration-->
<property name="eclipselink.connection-pool.default.initial" value="50" />
<property name="eclipselink.connection-pool.default.min" value="50"/>
<property name="eclipselink.connection-pool.default.max" value="100"/>
<property name="eclipselink.connection-pool.default.url" value="jdbc:jtds:sqlserver://SERVER/dev;instance=MSSQL2008R2"/>
<!-- Eclipselink cache configuration -->
<property name="eclipselink.cache.shared.default" value="true" />
<!-- Eclipselink logging configuration -->
<property name="eclipselink.logging.level" value="OFF"/> <!-- How much log should be shown | from: https://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging#Log_Level_Configuration-->
<property name="eclipselink.logging.level.sql" value="OFF"/> <!-- How to show the sql queries -->
<property name="eclipselink.target-database" value="SQLServer"/> <!-- What sql database is used | from: http://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/p_target_database.htm-->
<property name="eclipselink.ddl-generation" value="none"/>
<property name="eclipselink.session.customizer" value="platform.data.EclipseLinkSessionCustomizer"/> <!-- Defines a naming strategy -->
<property name="eclipselink.multitenant.tenants-share-emf" value="false"/>
...
Thank you for your help!

H2 and EclipseLink - very slow on tables with one row

I have 7 tables and some tables are empty and some has only one row. In table maximum 5 columns, no any extra indexes and I make quite simple SQL queries.
This is how I run h2.
java -jar h2-1.3.176.jar
Queries are generated and executed by eclipselink 2.6.3. These are my settings in persistence.xml
<properties>
<property name="eclipselink.connection-pool.default.initial" value="5"/>
<property name="eclipselink.connection-pool.default.min" value="5"/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost//home/somepath;DATABASE_TO_UPPER=FALSE"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.thread" value="false"/>
<property name="eclipselink.logging.exceptions" value="true"/>
<property name="eclipselink.orm.throw.exceptions" value="true"/>
<property name="eclipselink.left-join-fetch" value="true"/>
<property name="eclipselink.weaving" value="true"/>
</properties>
So every SQL query is executed about one second(!). Besides I hear that for every SQL query hard drive is working hard.
However, when I in H2 webconsole press connect to the same DB, then my java program works very fast. It works fast until I press disconnect in webconsole. How to explain this?

JPA PagingItemReader with PostgreSQL

I have done a job that reads data from a db and writes it in a file. It works fine with an Oracle DB. However, when I use it with Postgres I get the following error:
org.postgresql.util.PSQLException: ERROR: subquery in FROM must have an alias
Hint: Por ejemplo, FROM (SELECT ...) [AS] foo.
Position: 15
Error Code: 0
The reader is defined as follows:
<bean id="myReader"
class="org.springframework.batch.item.database.Jpa PagingItemReader">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="queryString" value="select c from CountryEntity c" />
<property name="pageSize" value="1000"/>
</bean>
Does anybody know if this is a common issue related with Postgres? Do I need to use a specific configuration?
You need to configure your JPA provider to use the PostGreSQL dialect.
E.g. for Hibernate, you would use a setup (persistence.xml) like this:
<persistence-unit name="somename" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:postgresql:sample"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>

How to Configure SSL over Database in Spring?

I want to add SSL security in the Database layer. I am using Struts2.1.6, Spring 2.5, JBOSS 5.0 and Informix 11.5. Any idea how to do this?
I have researched through a lot on the internet but could not find any solution.
Please suggest!
Here is my datasource and entity manager beans which is working perfect without SSL:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="INFORMIX" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.informix.jdbc.IfxDriver" />
<property name="url"
value="jdbc:informix-sqli://SERVER_NAME:9088/DB_NAME:INFORMIXSERVER=SERVER_NAME;DELIMIDENT=y;" />
<property name="username" value="username" />
<property name="password" value="password" />
<property name="minIdle" value="2" />
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="false">
<property name="targetObject" ref="dataSource" />
<property name="targetMethod" value="addConnectionProperty" />
<property name="arguments">
<list>
<value>characterEncoding</value>
<value>UTF-8</value>
</list>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" scope="prototype">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
Thankyou very much for your suggestion. So basically I need to set something like this in my applicationContext.xml, Please correct me if I am wrong:
<property name="username" value="username" />
<property name="password" value="password" />
**<property name="sslConnection" value="true" />**
<property name="minIdle" value="2" />
But how do I set the SSL certificate in java runtime. The link which you have provided is good but for some reason I am not able to follow. Please put your suggestion.
Using SSL for the communication between an application and a database is something that has to be supported by the database server (and the JDBC driver).
According to the documentation, this is supported by Informix Dynamic Server (IDS) since version 11.50.
You can use SSL support in your Java applications if you use IBM Data Server Driver for JDBC and SQLJ type 4 connectivity to DB2® for z/OS® Version 9 or later, to DB2 Database for Linux®, UNIX®, and Windows® Version 9.1, Fix Pack 2 or later, or to IBM Informix® Dynamic Server (IDS) Version 11.50 or later.
(...)
To use SSL connections, you need to:
Configure connections to the data source to use SSL. (link)
Configure your Java Runtime Environment to use SSL. (link)
The documentation should help.
If you're using a version of IDS prior to 11.50, then I'm afraid you'll have to use SSH tunneling.

Nhibernate 2.0.1 with mono

I've build my WinForm app on windows machine and the app is working
ok. When I user nhibernate 1.2.1 the app also worked on linux machine
using mono, but now when i upgraded app to nhibernate 2.0.1 it works
only in windows.
I've get error:
NHibernate.InvalidProxyTypeException: The following types may not be
used as proxies:
xxxx.Data.Dao.Credit : method obj_address should be virtual
......
Can anyone help me with this problem?
You can try and disable the NHibernate Config proxy validator. it seems to not work with mono.
You can do this by adding:
<property name="use_proxy_validator">false</property> in your app/web.config nhibernate section.
For an example config with this property set, see here:
http://www.mail-archive.com/nhusers#googlegroups.com/msg02109.html
or modify this:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<!--
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=YOUR_DB_SERVER;Database=Northwind;User ID=YOUR_USERNAME;Password=YOUR_PASSWORD;</property>
<property name="connection.isolation">ReadCommitted</property>
<property name="default_schema">Northwind.dbo</property>
-->
<!--
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SQLiteDriver</property>
<property name="connection.connection_string">Data Source=nhibernate.db;Version=3</property>
<property name="query.substitutions">true=1;false=0</property>
-->
<!-- mysql
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">Database=test</property>
-->
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
<property name="connection.connection_string">Server=localhost;database=test;User id=jrwren;password=yourpasswordhere.</property>
<property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
<property name="use_proxy_validator">false</property>
<!-- HBM Mapping Files -->
<mapping assembly="Test.exe" />
</session-factory>
</hibernate-configuration>
</configuration>
This might be of interest:
http://softwaredevscott.spaces.live.com/blog/cns!1A9E939F7373F3B7!251.entry
I'm also on mono trying to use NHibernate. Most forums seem to say setting the string to virtual will fix the problem, but this hasn't worked for me. What is curious is that my error is almost identical -
"" method obj_address should be virtual
This makes me think the proxy "address" is reserved for something else. Try changing the name of this column?