I have a simple query I want to perform in springboot. I am trying to return a document if a certain embedded document exists and I am searching for it by the value _id.
The syntax I am using is:
#Query("{customerServants._id:?0}")
TravelAgency findTravelAgency(int id);
This for some reason returns null eventhough only ids get passed to this query that actually exist in the database.
I checked it in mongodb with:
db.travel_agencies.find({"customerServants._id": 15})
which for my understanding should be the same query as the first one, but this one actually returns the right value(15 is just some value that actually gets passed to the spring data query).
What am I missing here?
Also this is the error I get:
java.lang.StackOverflowError: null
at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:721) ~[bson-4.6.0.jar:na]
at org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:449) ~[bson-4.6.0.jar:na]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:235) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:67) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:371) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:248) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:67) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:371) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:248) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:67) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:371) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:248) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:67) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:371) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:248) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:67) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:371) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:248) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.decode(ParameterBindingDocumentCodec.java:67) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
at org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec.readValue(ParameterBindingDocumentCodec.java:371) ~[spring-data-mongodb-3.4.0.jar:3.4.0]
Related
I'am setting up a simple Quarkus application using hibernate for testing Quarkus capabilities.
It contains a simple hibernate mapping
#Entity
Class AnEntity{
public double[] data;
}
It maps to the postgresql table anentity(data bytea).
This is fine for me and works well when running on JVM: zero problem on this side. I can read easily the content of the table using a NativeQuery.
Query query = em.createNativeQuery("select * from anentity",AnEntity.class);
List<AnEntity> resultList = query.getResultList();
for (AnEntity anEntity: resultList) {
System.out.println(anEntity.data);
}
The behavior here is that the content of the data array is serialized/deserialized using "standard" java serialization when reading/writing the data field from and to the database.
Now when compiling in native mode, I have a serialization related exception that follows:
ERROR [io.qua.run.Application] (main) Failed to start application (with profile prod): java.io.StreamCorruptedException: invalid type code: 40
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1712)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:2103)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1669)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:493)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:451)
at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:225)
at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:287)
at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:138)
at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:113)
at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:29)
at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:60)
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243)
at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:329)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:3214)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1887)
at org.hibernate.loader.Loader.hydrateEntityState(Loader.java:1811)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1784)
at org.hibernate.loader.Loader.getRow(Loader.java:1624)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:748)
at org.hibernate.loader.Loader.getRowsFromResultSet(Loader.java:1047)
I tried to register double[].class in a #RegisterForReflection annotation, changing double[] to Double[] but it did not help.
I'am not sure what's wrong here. It looks like the Java default serialization/deserialization is not working well in native mode, and I'am wondering how to solve this.
Thanks for any hints/pointer. I have the feeling it is a simple problem of a missing declaration or configuration parameter somewhere, at least I hope so :)
I'll double check known issues with Java deserialization in native mode.
Solved!
After more investigations it was a simple matter of registering the double[] class in native image builder serialization configuration file.
Added this line in the quarkus application.properties:
quarkus.native.additional-build-args = -H:SerializationConfigurationResources=serialization-config.json
and serialization-config.json being simply
[
{"name":"double[]"}
]
I just want to show a list of bank transaction. I tested the query in oracle and it works:
select bankDate, bankName from Bank where bankDate = '161222' group by bankDate, bankName
i also tried to use count to show the amount of the transaction that made on that day and it also works. select bankDate, bankName, count(bankName) as amount from Bank where bankDate = '161222' group by bankDate, bankName
but somehow when i use the query:
public List<Bank> listBank() {
List<Bank> list = (List<Bank>) getHibernateTemplate().find(
"from Bank where bankDate = '161222'" );
return list;
}
to my java project it went error like,
Hibernate: select bank0_.ID as ID0_, bank0_.BANKDATE as TRX2_0_, bank0_.BANKNAME as CARD3_0_ from mydb.mytable bank0_ where bank0_.BANKDATE='161222' group by bank0_.BANKDATE , bank0_.BANKNAME
Feb 05, 2020 11:56:12 AM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 979, SQLState: 42000
Feb 05, 2020 11:56:12 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ORA-00979: not a GROUP BY expression
Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:630)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:921)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:913)
at com.jalin.bank.dao.impl.BankDaoImpl.listBank(BankDaoImpl.java:44)
at com.jalin.bank.bo.impl.BankBoImpl.listBank(BankBoImpl.java:41)
at com.jalin.common.App.main(App.java:27)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:930)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
... 6 more
Caused by: java.sql.SQLSyntaxErrorException: ORA-00979: not a GROUP BY expression
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:225)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:53)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:774)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:925)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1111)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4798)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:4845)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1501)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
at org.hibernate.loader.Loader.doQuery(Loader.java:674)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
... 15 more
i tried to use some of the example that are on internet, but it just wont work with me..
i also tried to use <property ... formula="count(bankName)"></property> for count, but it still won't do me any favor.
if someone can help me to work with hibernate are very welcome, i'm not really familiar with this. because i'm very new with spring mvc+maven+hibernate.
From the documentation :
You are fetching two things in the query you are running manually whereas the one hibernate runs it is fetching three things. The error is because you need to group on all the columns that you use in select clause
ORA-00979 occurs when the GROUP BY clause does not contain all the expressions in the SELECT clause. Any SELECT expression that is not included in the GROUP function must be listed in the GROUP BY clause. These are AVG, COUNT, MAX, MIN, SUM, STDDEV, and VARIANCE. You may have also tried to execute a SELECT statement that contains a GROUP BY clause.
I'm trying to get hibernate's ResultTransformer to work for fetching an entire table but keep getting a NullPointerException.
List result = session.createQuery(
"FROM com.maps.model.Book")
.setResultTransformer( Transformers.aliasToBean(Book.class))
.list();
Is there a way to get this to work for an entire table using HQL?
(The reason I'm doing this is that I have a project with 50 plus tables which I need to cache and I don't want to write 50 different methods.)
EDIT: the exception stacktrace:
java.lang.NullPointerException
at org.hibernate.transform.AliasToBeanResultTransformer.initialize(AliasToBeanResultTransformer.java:116)
at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:85)
at org.hibernate.hql.internal.HolderInstantiator.instantiate(HolderInstantiator.java:95)
at org.hibernate.loader.hql.QueryLoader.getResultList(QueryLoader.java:438)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2263)
at org.hibernate.loader.Loader.list(Loader.java:2258)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1161)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at com.maps.manager.DatabaseManager.findAll(DatabaseManager.java:65)
at com.maps.manager.DatabaseManager.main(DatabaseManager.java:40)
For some odd reason when I execute a mysql stored procedure via hibernate it interprets a string "N/A" as a double value and throws an error. When running the same query in mysql directly everything works fine, but Hibernate seems to think I'm trying to divide? If all things left the same "NA" the stored procedure runs with no problem via hibernate.
Any idea how to get the sting to be accepted in hibernate?
The call:
def query = session.createSQLQuery("call myproc (:id, :date)")
.setParameter("id", 168)
.setParameter("date", "2015-02-01").executeUpdate()
The part of the stored procedure:
insert table1
( myvarchar255field)
( select "N/A"
from table2
where [something else happens]);
org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTy>peDelegate.java:69)
at
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
at
org.hibernate.engine.query.spi.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:211)
at
org.hibernate.internal.SessionImpl.executeNativeUpdate(SessionImpl.java:1310)
at
org.hibernate.internal.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:389)
at
Caused by:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated
incorrect DOUBLE value: 'N/A' at
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3885) at
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823) at
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435) at
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) at
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2530) at
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1907)
at
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2141)
at
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2077)
at
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2062)
at
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
... 4 more
I want to generate UUID for a unique string, i am using the following code:-
thread.createSession();
HexGenerator gen1 = new HexGenerator();
gen1.setHexId("2");
thread.ses.save(gen1);
gen1 = (HexGenerator) thread.ses.load(HexGenerator.class, gen1.getHexId());
System.out.println("gen1-->" + gen1.getHexId());
thread.commit();
Below is my hibernate file:-
<class name="entity.HexGenerator" table="dual">
<id name="hexId" type="string" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
</class>
UUId is generated properly, but i am getting an error when the complete transaction is committed. as in the following error comes.
Exception in thread "main" - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.ofss.test.hibernate.HibernateThread.commit(HibernateThread.java:29)
at com.ofss.test.hibernate.HibernateThread.main(HibernateThread.java:57)
Caused by: java.sql.BatchUpdateException: ORA-00904: "HEXID": invalid identifier
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10698)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 9 more
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.ofss.test.hibernate.HibernateThread.commit(HibernateThread.java:29)
at com.ofss.test.hibernate.HibernateThread.main(HibernateThread.java:57)
Caused by: java.sql.BatchUpdateException: ORA-00904: "HEXID": invalid identifier
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10698)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
You can't insert or update on the Oracle dual virtual table. You just can use it at select queries.
So, your line thread.ses.save(gen1) is what generates the error. I think that if you just delete this line, the error will be fixed.
But I don't really understand why you need to access the database to generate your UUID, which seems to be generated at the HexGenerator class (or maybe at the "uuid.gen" class referenced at the hibernate file).
To overcome the above problem, i mean to generate the UUID, now I have decided to use two method, either to use java.util.UUID.randomUUID() or to write my own logic to generate the same using bits array and then randomizing it.
first, your code looks a little bit strange. You create instance of HexGenerator:
HexGenerator gen1 = new HexGenerator();
use it and immediately override using some unknown for me API that uses dynamic class loading.
thread.ses.load(HexGenerator.class, gen1.getHexId())
Then you call getHexId() again. I believe that code
new HexGenerator().getHexId()
will generate ID that you need.
But I think it is not yet the reason for failure. Unfortunately you did not provide any information about you DB schema. I believe that you ID is simply defined as number, it cannot accept strings. Check it again and please provide more details if this is not the reason.