Spring And Hibernate [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am new to Spring MVC and hibernate, I have two Entity Category and Sub_Category, Sub_category contains Foreign Key(Category_id) how can i insert value in Sub_Category only with the value of Foreign Key.
I my case values are inserting in Sub_category and as well creating new row in Category table and getting the value of that newly creating row id and saving in Sub_Category table but instead i want the value of already existed id value of Category table and store it in sub_Category Foreign Key column.

Let's assume your object is category
after saving it as
category.save(); // after the database insertion
create another object of Sub_Category as
if(category.save()){ // I assume, saved returns true on successful insertion
Sub_Category subCategory = new Sub_Category();
subCategory.setCategory(category); // category that you saved above
subCategory.save();

Related

Count the number of existing values in a field in a record [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
Such as I have a table of User---(id,name,age,hobby,sports)
Then it has a record that (1,"zhangsan",18,null,null);
Next you can see the total of record is 3, because hobby and sports are null.
use SQL sentences to count the number of existing values in a field in a record
Help and thanks
Use CASE expressions to count each column.
SELECT
CASE WHEN Column1 IS NOT NULL THEN 1 ELSE 0 END
+ CASE WHEN Column2 IS NOT NULL THEN 1 ELSE 0 END
-- repeat for all columns
AS NumberOfNonNullColumns
FROM MyTable

How to override duplicate keys in Java Map [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this as my query.
select a.cust_id,a.cust_name,b.cust_id,b.cust_name
from acustomer a,bcustomer b
DaoImp using Spring NamedJdbcparameterTemplate
method:
NamedJdbcparameterTemplate temp= new NJPT(datasource);
List<Map<String,Object>> out=temp.quertForList(query,parametermap);
But the problem is that whenever I get the output for this query in db tool, I get 4 columns but in program output I am only getting 2 columns, i.e cust_id and cust_name of a is getting overridden by b due to same key name in Map.
How can I fix this, please note the query will be different each time as I am using this method as a general one for my program and output will be a list of values, so cannot map any model class for the output.
Please note I want this function to be generic one which means the query will be changing each time and output will be of different types.
Well the easy solution would be to give your fields aliases, so that their keys would be different.
select a.cust_id a_cust_id, a.cust_name a_cust_name, b.cust_id b_cust_id, b.cust_name b_cust_name
from acustomer a, bcustomer b
where a.cust_id=b.cust_id
Then in the map you would find the aliases, a_cust_id, a_cust_name, b_cust_id, b_cust_name.

Hibernate Criteria Query with Predicates to select records from given list of Ids [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to write a Hibernate Criteria query with Predicate to fetch records from db from a given list of ids, where id is a column in db table.
What I've done is
List<Predicate> predicates = new ArrayList<>();
for(String id : ids)
{
predicates.add(builder.equal(root.get(MyModel.id), Integer.parseInt(id)));
}
//Other predicates I don't care about
Predicate queryPredicate = builder.and(predicates.toArray(new Predicate[predicates.size()]))
criteriaQuery.select(root);
criteriaQuery.where(queryPredicate);
I get 0 result
I think id is unique for each row . you have to use OR instaed of AND
like
Predicate queryPredicate = builder.or(predicates.toArray(new Predicate[predicates.size()]))
It is logically false that the records who have all ids.
OR
you have to use CriteriaBuilder.in() method for matching the records for exact ids you are giving.

How to find number of occurrence of a specific value in table column which contain pipe delimiter in MySQL query? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How to find number of occurrence of a specific value in table column which contain pipe delimiter in MySQL query?
Please refer the below string:
0|5573|73|5573|73
I want the count of 73 in above string it should give answer as 2.
-----------------------------
column -- count
------------------------------
0|5573|73|5573|73 -- 2
Please refer below mysql query which will return no of occurence of specific value as per your question.
In below query test_table is a table which contain the column named 'test_colum' which contains values like '0|5573|73|5573|73' and you want to find the no of occurrences of value '73' in it.
SELECT
test_colum,
(IF((POSITION('73|' IN test_colum))=1,1,0))
+
( ROUND (
(
(LENGTH(test_colum)
- LENGTH( REPLACE ( test_colum, "|73|", ""))
)
) / LENGTH("|73|")
)
+
IF((substring_index(test_colum,'|',-1) = '73'),1,0)) AS value_cnt
FROM test_table;
It will produce result as following :
test_colum value_cnt
0|5573|73|5573|73 2

If column doesn't contain one of the strings, set to null [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
what is the best way to compare the elements of several columns in MySQl table to the elements of string array?
If the elements are not equal then et the element of the current column to null.
Thanks!
Multiple update statements to do multiple columns, but the below might help:
UPDATE myTable SET myColumn1 = null WHERE myColumn1 NOT IN ('item1', 'item2', 'item3');
UPDATE myTable SET myColumn2 = null WHERE myColumn2 NOT IN ('item1', 'item2', 'item3');
UPDATE SOMETABLE SET SOMECOLUMN = null WHERE SOMECOLUMN=array[0] OR SOMECOLUMN=array[1] etc.

Categories

Resources