Following documentation of spring-data that is available here there is possibility of creating custom queries using naming convention of methods.
I can find record by some value in column1 using findByColumn1 and find objects that contains NULL in column2 by using findByColumn2IsNull, but when I try to do something like
MyResult result = findByColumn1AndColumn2IsNull("expectedValueOfColumn1");
it returns results that contains column2 property that is set to non-null value.
Can somebody tell me if I'm using the query method wrong, or there is some other issue here?
Related
I'm executing stored procedure with Hibernate native method, this stored procedure creates column names depending on another table Ids. So its columns look like something like this:
| id | ... some other columns ... | name | c_1 | c_2 | c_4 | c_.. |
If I call Query.getResultList() it returns List<Object[]>, and I don't know column names. I have to know column names (and corresponding column index) to continue my further business logic. I also cannot use EntityManager.createNativeQuery(String s, Class aClass) since it is not one POJO class.
Currently I'm getting List<Object[]> without problem, but I need, for example, Map<String,Object[]> column name as a key and column values as an array of Objects.
How can I get all column names with their values?
Finally, found the solution, and I'm going to share with you(maybe helps someone). Here is what I did:
I added Spring JDBC Support to my application, then,
I used Spring SimpleJdbcTemplate Querying, there is a method: SimpleJdbcDaoSupport.getSimpleJdbcTemplate().queryForList(nativeQuery) which returns List<Map<String,Object>>
(This is just an idea, I haven't tested it. And it needs Criteria API.)
If you manage to get the result as List<javax.persistence.Tuple>, you can take one Tuple and call getElements() on it - you'll get a list of TupleElement<?>s on which you can call getAlias(). Presumably this will be the column name. But I'm not 100% sure and it won't work if there are no tuples (results) returned, but I think it's worth a try. See 9.2. Tuple criteria queries.
For this approach, this might help:
Calling stored procedure from Java / JPA
How can i call stored procedure using spring with jpa
Since the play documentation on models is terrible I'll ask here. I have the basic code;
public static void Controller() {
List<Item> item = Item.find("SELECT itemname,id FROM Item WHERE itembool = true ORDER BY itemcreated ASC LIMIT 0,1").fetch();
if ( item == null ) {
notFound();
}
}
What I'm trying to do is get the value for 'itemname' returned for the first value returned from an SQL query (The real query is much more complicated and other things so it can't be replaced with methods). I can get the entire first object with item.get(0) but I can't figure out how to get the value of 'itemname' as a string and it doesn't seem to be documented anywhere.
Edit
Probably should have mentioned in the original question, I need to retrieve by field name, not index. I.E. I can't do items.get(0)[0]; I need to do items.get(0)['itemname'];
The documentation explains this if you read it, in here. Hibernate doesn't use SQL, but JPQL, which has a different syntax as it works with objects, not individual fields.
What you want to do can be achieved in two ways (both in the documentation):
List<Item> item = Item.find("SELECT i FROM Item i WHERE i.itembool = true ORDER BY i.itemcreated ASC").fetch(1);
List<Item> item = Item.find("itembool = true ORDER BY itemcreated ASC").fetch(1);
EDIT:
On the retrieval part, you will get a list of Item, so you can just access the field directly on the object:
item.get(0).getItemName();
Since Play uses Hibernate under the hood, you need to take a look at Hibernate's documentation.
In particular, SELECT itemname,id ... yields Object[] rather than Item, so that you can get itemname as follows:
List<Object[]> items = ...;
String itemname = items.get(0)[0];
well if you have to do a select itemname,id ..., you would not be able to do a items.get(0)["itemname"] because as axtavt and Pere have mentioned, you would get a Object[] back. You can instead create another (perhaps immutable) entity class that can be used in this query. Please refer to hibernate documentation for details. You can then model the entity based on your query requirements and use it to fetch information, thus letting hibernate handle all the magic number game for you. That ways, you would have a bean with filled up values that you can use to map back to your model class if you like.
HTH!
I have a table (Oracle database) that looks like:
CREATE TABLE example
(
idEx INTEGER,
idAdh INTEGER,
date DATE,
PRIMARY KEY (idEx, idAdh)
)
I have generated the corresponding classes in Netbeans and I have two classes created for this table: Example.java and ExampleId.java, this last one containing the two values of my primary key.
Now let's say I have some records here and I would like to delete one using only one value of the primary key (idEx for example, which is unique too). So first I need to get that row, but I can't find a way to do this. Would it be possible to do something like this?
Example ex = (Example) session.get(Example.class, new ExampleId(?, idEx));
I'd need something to replace that ? that would act as a wildcard.
Or maybe this is absolutely not the way to go and in this case I'd really appreciate some advices.
You should be able to get the ex with a HQL query.
There is no way to get this without an HQL/JPQL/Criteria query, for good reason.
The get method returns a single object and automatically generates a query like select * from table where key = :key.
The only way you can guarantee that this query returns exactly one row is when you specify the entire key. If you don't have the full PK object available, the query will return a list of objects, at which point get is not appropriate anymore.
What is the uses of setNull() method in PreparedStatement interface? I looked in this post.
It says: Without the setNull(..) method there would be no way to set null values for the Java primitives.
however with autoboxing in JDK5, I think null values can be set on even primitive types.
There is another post in some other forum says:If you want to be portable to different databases, use the setNull() method.
However there is nothing clearly mentioned in Java doc. Could you help me understanding this?
I think it's easier to understand this if you view it from the database end. If you want to set a field to NULL in your database insert statement, then you need a way of telling the database that is should be set to NULL rather than the default value for the column. If in the database schema you have a nullable integer field, you would use set null to set it to the DB NULL value, rather than to its default value ( 0 ).
I want to pass a Set of Strings in an iBatis query for the parameter map as well as return a collection of strings for the result set.
Is this possible?
Example queries ...
SELECT * FROM some_table t WHERE t.some_column IN (values);
UPDATE some_table t SET t.some_column = 'some_value' WHERE t.other_column IN (values);
Walter
If you want to pass a List of Strings as one parameter, for example for building a IN(val1,val2...) query, then you should read about dynamic queries, in particular the Iterate element. See also.
For the return, In SqlMapClientTemplate there is the queryForList method.
As for the Set of String as parameter, I do not know if iBatis handles that; we built an object for that, and when I faced that problem it was in a sql in clause, so I made a loop with comma separated values.
Or you can convert the Set to an HashMap and pass that.