How to use HSQLdb with Ibatis - java

I want to use in memory database to query for data in my unit testing, my project is Ibatis (with annotation) for querying actual database which I want to mimic with the help of HSQLDB.
Please help me with how to configure iBatis with HSQLDB.
Also is there any way to these better for unit testing with code which is strongly dependent on database in its functions.

You can make an iBatis sqlMappings.xml config file something like this:
<sql-map-config>
<properties resource="configuration.properties" />
<!--The datasource for you application is configured here: -->
<datasource name = "hsql"
factory-class="com.ibatis.db.sqlmap.datasource.SimpleDataSourceFactory"
default="true">
<property name="JDBC.Driver" value=""/>
<property name="JDBC.ConnectionURL" value=""/>
<property name="JDBC.Username" value=""/>
<property name="JDBC.Password" value=""/>
</datasource>
<!--Declare the SQL Maps to be loaded for this application.
Be sure it's in your classpath. -->
<sql-map resource="maps/beanMappings.xml"/>
</sql-map-config>
plus a congifuration.properties file like this:
JDBC.Driver=org.hsqldb.jdbcDriver
JDBC.ConnectionURL=jdbc:hsqldb:hsql://localhost/myDb
JDBC.Username=sa
JDBC.Password=
and then use it like this:
String resource = "maps/SqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlMap sqlMap = XmlSqlMapBuilder.buildSqlMap(reader);

Related

How can I configure JPA for a postgres database schema?

I have an existing postgres database with the database "testdb" and the database schema testdbschema".
If I use the default persistence.xml configuration of RESOURCE_LOCAL the following property is working:
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://server:port/testdb?currentSchema=testdbschema" />
I want to configure my database connection within my web.xml as a data-source. Everything is working well, except the configuration of the database schema.
Here is my web.xml configuration:
<data-source>
<name>java:global/myapp</name>
<class-name>org.postgresql.ds.PGSimpleDataSource</class-name>
<server-name>127.0.0.1</server-name>
<port-number>5432</port-number>
<database-name>testdb</database-name>
<user>postgres</user>
<password>postgres</password>
</data-source>
Do you now how can I configure my db schema name here?
The configuration via testdb?currentSchema=testdbschema did not work for me and I get a database not found failure.
The solution was found within the PGSimpleDataSource:
<data-source>
<name>java:global/myapp</name>
<class-name>org.postgresql.ds.PGSimpleDataSource</class-name>
<server-name>127.0.0.1</server-name>
<port-number>5432</port-number>
<database-name>testdb</database-name>
<user>postgres</user>
<password>postgres</password>
<property>
<name>currentSchema</name>
<value>testdbschema</value>
</property>

Hibernate: create SQL Server 2012 database table in non-default schema

I'm trying to use Hibernate to create a table in a SQL Server 2012 database.
I set a number of properties in my Java project's persistence.xml file:
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2012Dialect" />
<property name="hibernate.hbm2ddl.auto" value="auto" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.use_sql_comments" value="false" />
... and set the database connection details in when UI create my data source, which is done in a configuration Java class:
dataSource.setDriverClassName(myDriver); // com.microsoft.sqlserver.jdbc.SQLServerDriver
dataSource.setUrl(myUrl); // jdbc:sqlserver://host:port;DatabaseName=dbname
dataSource.setUsername(myUser);
dataSource.setPassword(myPwd);
In the Java class, I also set the default schema when I create the Java Persistence entity manager factory:
final Map<String, Object> jpaProperties = new HashMap<>();
jpaProperties.put("hibernate.default_schema", "mySchema");
entityManagerFactory.setJpaPropertyMap(jpaProperties);
entityManagerFactory.afterPropertiesSet();
return entityManagerFactory.getObject();
The Java domain class has been annotated as follows:
#Entity
#Table(name = "mytable")
public class MyTable {
...
}
The schema called "mySchema" has been created in the SQL Server database and authorization has been granted to the user specified in the data source.
When I try and run the functionality which should create the table as part of the first run (and populate it appropriately), I get the following error message:
SQLServerException: Invalid object name 'mySchema.mytable'
... and the table has not been created in the schema.
The Hibernate SQL looks like this:
select alias0_.ID as alias1_0_0_
... other columns...
from mySchema.mytable alias0_
where alias0_.ID=?
I'm using version 1.0.0.Final of the hibernate-jpa-2.1-api JAR in my Java project.
Thanks in advance for any assistance.

Wildfly Data Persistence

I am currently working on a Java EE project and am working with the Wildfly server.
I have a Web project and EJB project which are deployed onto the Wildfly server.
I can save a user for example, but only for as long as the server is running.
There is no data persistence between server downtimes.
I have searched through the internet but couldn't find an answer.
My persistence.xml looks like this:
<persistence-unit name="primary">
<!-- If you are running in a production environment, add a managed
data source, this example data source is just for development and testing! -->
<!-- The datasource is deployed as WEB-INF/kitchensink-quickstart-ds.xml, you
can find it in the source at src/main/webapp/WEB-INF/kitchensink-quickstart-ds.xml -->
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="false" />
<value="true"/>
</properties>
If I want to persist any information, do i need to reconfigure this file?
I hope you can help me :)
Your problem is this line
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
Everytime when the wildfly starts up, JPA creates a new database model with an empty database.
Adjust your code to
<property name="hibernate.hbm2ddl.auto" value="update" />
You are using "ExampleDS" which is set up as H2 in-memory database by default. It therefore does not persist data between restarts on purpose (useful for development/testing). Go to wildfly's standalone/configuration/standalone.xml configuration file and search for "ExampleDS" in the "datasources" section. It should show:
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
where "mem" means in-memory. You can change "mem:test" to any write path, e.g.
<connection-url>jdbc:h2:~/test;DB_CLOSE_DELAY=-1</connection-url>
to use a H2 file-based database stored as "test" in your home-folder (assuming *nix).
You can also define additional databases (Postgresql, Oracle, etc) in the datasources-section.

Using/converting an XML file to a Spring bean definition

I have defined a Spring application context xml which will be edited by end users to add new beans.Something like:
<bean name="myBeanName1" class="com.xxx.Yyy">
<property name="type" value="type1" />
<property name="name" value="name1" />
<property name="arguments" value="arg1" />
</bean>
<bean name="myBeanName2" class="com.xxx.Yyy">
<property name="type" value="type2" />
<property name="name" value="name2" />
<property name="arguments" value="arg2" />
</bean>
.
.
.
Now I am asked to change this to a normal xml so that users wont be bothered by bean property and class names:
<def name="Name1">
<type>type1</type>
<argument>arg1</argument
</def>
<def name="Name2">
<type>type2</type>
<argument>arg2</argument
</def>
As my code is using the bean, how can I use the new xml with minimal code change so that it is converted to a bean definition as earlier and things work as before?.
I dont know if Spring has a solution for this out of the box. What I thought was applying stylesheet to the new xml to create my older bean. Any other better/elegant solution for this?
Edit: Now that user input is not inside the bean anymore, is it possible to use the new xml to inject to a #Component class?
Spring supports creating custom tags. You need to create xsd schema, NamespaceHandlerm, implement BeanDefinitionParsers and make spring aware of these by creating spring.handlers & spring.schemas special files.
Have a look at Extensible XML authoring
Example:
<beans xmlns declaration goes here>
<yournamespace:yourcustomtag id="some id">
<yournamespace:some_other_tag your-custom-attribute="some value" />
</yournamespace:yourcustomtag>
</beans>

Using configuration.xml file and supplying DataSource settings in MyBatis 3.0.6

MyBatis documentation shows a way to build a SqlSessionFactory through XML configuration file or a Configuration object through Java code. It also mentions passing properties that will override what is specified in the XML file.
I am trying use the properties facility to supply a data source URL in addition to the XML configuration, but the property does not get set.
Here's the configuration XML I am using (removed all the aliases and mappings for brevity):
<configuration>
<typeAliases>
<typeAlias alias="Item" type="com.example.project.Item"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:C:/path_to_db_file_in_the_filesystem"/>
<property name="username" value="sa"/>
<property name="password" value="sa"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/project/mappers/ItemMapper.xml"/>
</mappers>
</configuration>
The XML file works fine for everything, but I need to supply the url through Java code (through the code, it is obtained from another configuration file so it has to be dynamically supplied to MyBatis). I know I can do it all in code, but that will be unnecessary work so I would like to avoid that route if possible.
Based on the description in the manual, I removed the url line from the XML file and came up with the following code:
String resource = "com/example/project/MyBatisConfiguration.xml";
Reader reader = Resources.getResourceAsReader(resource);
Properties props = new Properties();
props.setProperty("url", url);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader, props);
The code builds the SqlSessionFactory, but I get the "url cannot be null" exception, indicating that the property was not successfully overwritten. I think the name of the property should be in a special format, but I could not find out what that format is.
Thanks a lot in advance for all the help.
In your configuration XML you can insert a placeholder for the datasource URL like that:
<property name="url" value="${url}"/>
Then the build() method of SqlSessionFactoryBuilder replaces "${url}" in the XML with the value of property "url" provided by props.

Categories