I need to just fetch some geospatial data (simple coordinates - POINTS) from MySQL db and use them in Java (NetBeans project). This is what I did so far:
Created MySQL db with following table:
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lokacija` point DEFAULT NULL,
`info` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
I also created NetBeans project and set up JPA and everything, added hibernate-spatial library to it and here is my persistence.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="testGisPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>model.Test</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:8889/dipl?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="root"/>
</properties>
</persistence-unit>
</persistence>
After that, created entity and besides all other properties, it also contains following:
...
#Column(name = "lokacija")
#Type(type="org.hibernate.spatial.GeometryType")
private Point lokacija;
#Column(name = "info")
private String info;
...
I also generated REST facade directly from NetBeans and I tried to print all items I fetch from the db (currently I have only two items with descriptions "first" and "second" and locations (1,1)) and here is what I get:
LOCATION: null
INFO: first
LOCATION: null
INFO: second
Any suggestions what else I should do in order to make this work?
Related
I am trying to execute stored procedure using hibernate. I face following issue. Starting to learn hibernate, and the proc is very simple, but not able to identify the issue.
Any guidance how i can resolve this, will be helpfull.
Stored Proc:
CREATE PROCEDURE `GET_TOTAL`(IN sno int, OUT count_out int)
BEGIN
SELECT COUNT(*) into count_out from table2 WHERE sno = sno ;
END
Java:
package database.hibernate_v2.hibernate_v2;
import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureQuery;
public class App4 {
public static void main( String[] args )
{
EntityManager entityManager = JPAUtility.getEntityManager();
entityManager.getTransaction().begin();
StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("GET_TOTAL");
// set parameters
storedProcedure.registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN);
storedProcedure.registerStoredProcedureParameter(2, Integer.class, ParameterMode.OUT);
//storedProcedure.registerStoredProcedureParameter("out1", Integer.class, ParameterMode.OUT);
storedProcedure.setParameter(1, 1);
//storedProcedure.setParameter("out1", 1);
storedProcedure.execute();
}
}
persistence.xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="agisdb">
<description>JPA Demo</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/testDB1?useSSL=false"/>
<property name="javax.persistence.jdbc.user" value="xxxxx"/>
<property name="javax.persistence.jdbc.password" value="xxxxx"/>
</properties>
</persistence-unit>
</persistence>
Error: The parameter 2 for the stored proc is "OUT" parameter, but not sure why i get error message "Parameter number 2 is not an OUT parameter".
ERROR: Parameter number 2 is not an OUT parameter
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Error preparing CallableStatement
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.procedure.internal.ProcedureCallImpl.buildOutputs(ProcedureCallImpl.java:456)
at org.hibernate.procedure.internal.ProcedureCallImpl.getOutputs(ProcedureCallImpl.java:404)
at org.hibernate.procedure.internal.ProcedureCallImpl.outputs(ProcedureCallImpl.java:663)
at org.hibernate.procedure.internal.ProcedureCallImpl.executeUpdate(ProcedureCallImpl.java:680)
at database.hibernate_v2.hibernate_v2.App4.main(App4.java:23)
Caused by: java.sql.SQLException: Parameter number 2 is not an OUT parameter
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.CallableStatement.checkIsOutputParam(CallableStatement.java:630)
at com.mysql.jdbc.CallableStatement.registerOutParameter(CallableStatement.java:1807)
at org.hibernate.procedure.internal.AbstractParameterRegistrationImpl.prepare(AbstractParameterRegistrationImpl.java:291)
at org.hibernate.procedure.internal.ProcedureCallImpl.buildOutputs(ProcedureCallImpl.java:444)
... 4 more
I am setting up Arquillian with OpenEJB. I want to test a webservice with access to a database. I was able to setup everything with my own bean and entity for testing purposes.
Now I am adding all classes and dependencies and trying to start the test. But it fails to deploy, because my entities have an schema defined in its #Table annotation.
arquillian.xml
<arquillian xmlns="http://jboss.org/schema/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://jboss.org/schema/arquillian
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<engine>
<property name="deploymentExportPath">target/deployments</property>
</engine>
<container default="true" qualifier="openejb-embedded-4">
<configuration>
<property name="properties">
testDatabase = new://Resource?type=DataSource
</property>
</configuration>
</container>
</arquillian>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="newejb" transaction-type="JTA">
<jta-data-source>testDatabase</jta-data-source>
<class>[...].AuftragsHistorie</class>
<properties>
<property name="openejb.jpa.init-entitymanager" value="true" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
</persistence>
AuftragsHistorie.java
#Entity(name = "AuftragsHistorie")
#SequenceGenerator(
name = "AUFTRAGSHISTORY_GENERATOR",
sequenceName = "SEQ_T_PUB_INET_DAF",
allocationSize = 1
)
#Table(
name = "T_PUB_INET_DAF",
schema = "DATA_SCHEMA"
)
public class AuftragsHistorie implements Serializable {
last part of stacktrace
Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: invalid schema name: DATA_SCHEMA in statement [CREATE TABLE DATA_SCHEMA.T_PUB_INET_DAF (ID BIGINT NOT NULL, AUFTRAGS_ART_NAME VARCHAR(255) NOT NULL, ERF_DATUM TIMESTAMP NOT NULL, VERS_NR VARCHAR(255) NOT NULL, PRIMARY KEY (ID))] {stmnt 2112059634 CREATE TABLE DATA_SCHEMA.T_PUB_INET_DAF (ID BIGINT NOT NULL, AUFTRAGS_ART_NAME VARCHAR(255) NOT NULL, ERF_DATUM TIMESTAMP NOT NULL, VERS_NR VARCHAR(255) NOT NULL, PRIMARY KEY (ID))} [code=-4850, state=3F000]
at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:218)
at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:202)
at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access$700(LoggingConnectionDecorator.java:58)
at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingStatement.executeUpdate(LoggingConnectionDecorator.java:913)
at org.apache.openjpa.lib.jdbc.DelegatingStatement.executeUpdate(DelegatingStatement.java:118)
at org.apache.openjpa.jdbc.schema.SchemaTool.executeSQL(SchemaTool.java:1231)
at org.apache.openjpa.jdbc.schema.SchemaTool.createTable(SchemaTool.java:976)
at org.apache.openjpa.jdbc.schema.SchemaTool.add(SchemaTool.java:552)
at org.apache.openjpa.jdbc.schema.SchemaTool.add(SchemaTool.java:364)
at org.apache.openjpa.jdbc.schema.SchemaTool.run(SchemaTool.java:341)
at org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:505)
... 109 more
I already tried adding the schema via #CreateShema annotation and adding a lot of different properties, but nothing solved the problem.
Arquillian starts a HSQL Database.
I ended up using an Arquillian extension like this:
https://github.com/arquillian/arquillian-examples/tree/master/arquillian-lifecycle-extension-tutorial
I start a H2 database before the deployment, create the required schemata and shut it down after undeployment.
I'm trying to use JPA in Wildfly (which uses Hibernate) with PostgreSQL. I turned on hibernate.show_sql so I can execute the same query in PgAdmin III.
The query is this one (EDIT: issuemanager_sch is the schema. Without it even in PgAdmin the query fails with "relation does not exist" error):
select issuetypet0_.id_issue_type as id_issue1_3_, issuetypet0_.name as name2_3_ from issuemanager_sch.issue_type issuetypet0_
When executed by Hibernate this is showed in both PostgreSQL log and Wildfly log :
relation "issuemanager_sch.issue_type" does not exist character 87
However, when I copy (to grant I'm executing the exact same query), paste and run the query in PgAdmin III it's executed normally and I get the results.
What could possibly be wrong with my configurations? (I tried with both quoted and unquoted queries and the results are the same: fails for Hibernate, works in PgAdmin III)
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
<persistence-unit name="PersistenceManager" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>java:/datasources/issuemanager_pg</non-jta-data-source>
<class>com.im.issuerepository.domain.IssueTO</class>
<class>com.im.issuerepository.domain.IssueTypeTO</class>
<class>com.im.userrepository.domain.UserTO</class>
<class>com.im.userrepository.domain.GroupTO</class>
<class>com.im.userrepository.domain.RoleTO</class>
<validation-mode>NONE</validation-mode>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.default_schema" value="issuemanager_sch"/>
</properties>
</persistence-unit>
</persistence>
EDIT: issue_type table:
CREATE TABLE issuemanager_sch.issue_type
(
id_issue_type bigint NOT NULL DEFAULT nextval('issuemanager_sch.issue_type_seq'::regclass),
name text NOT NULL,
CONSTRAINT "PK_ISSUE_TYPE" PRIMARY KEY (id_issue_type)
)
EDIT: IssueTypeTO mapping:
#Entity
#Table(name = "issue_type")
public class IssueTypeTO implements Serializable{
#Id
#Column(name = "id_issue_type")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "issue_type_seq")
#SequenceGenerator(name = "issue_type_seq", sequenceName = "issue_type_seq", allocationSize = 1)
private Long idIssueType;
#Column(name = "name", length = 45)
private String name;
// getters and setters
}
I have two different java web application than access a shared postgres database for reading and writing.
My first web application in deployed on apache tomcat and my second web application is deployed on jboss.
When one of my two application write a new row on a table of database, the sequence is incremented of 1 and its value is assigned to primary key and row is stored correctly.
When the other application try to write a new row on the same table, the id than was assigned is not synchronized with sequence, and I have exception for duplicate primary key.
In my java class I use this annotation for define my id:
#Id
#SequenceGenerator(name = "my_seq", sequenceName = "my_seq_on_db", allocationSize=1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_seq")
Long my_id;
this is my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<persistence-unit name="MyPersistenceUnit" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/myDatasouce</jta-data-source>
<class>myPackage.myClass</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
I have an EJB Project deployed on JBoss with some entity classes.
As soon as I deploy the EJB Jboss gives me this exception:
create table service_db (id integer not null auto_increment, ipAddress varchar(255),
primary key (id)) type=InnoDB
Unsuccessful: create table service_db (id integer not null auto_increment,
ipAddress varchar(255), primary key (id)) type=InnoDB
2011-08-17 11:38:59,997 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1....
Shouldn't it be engine=InnoDB instead of type? I tried to execute this statement in mysql without luck (same error). If I remove or replace type with engine it is working.
My question is, who can I change that?
Here is my persistance.xml (in case it is important)
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="default">
<jta-data-source>java:/DUMMY_DS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
</properties>
</persistence-unit>
and in case the datasource xml file is important:
<datasources>
<local-tx-datasource>
<jndi-name>DUMMY_DS</jndi-name>
<connection-url>jdbc:mysql://localhost/cargosoft</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>hauke</user-name>
<password>******</password>
<min-pool-size>5</min-pool-size>
<max-pool-size>20</max-pool-size>
<idle-timeout-minutes>10</idle-timeout-minutes>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
</local-tx-datasource>
</datasources>
Thanks for your help.
Hauke
Use MySQL5InnoDBDialect instead.