Loading a Single column out of 50 Using Load() of Hibernate - java

Suppose there are 50 columns in database table. Each column contains image(binary data). You need to display only one one image out of 50 at a time. How would you achieve the same via hibernate? Keep in mind that executing load on the table will result in loading complete row while we need only one column data.

Pavnesh, I am answering the question in a geenral fashion. If you just one one column from the table create a named query and return that single column, and retrieve the data in your DAO class with proper datatype.
and if you want just a single row use query.setMaxresult function and give the value as 1 and then you can select the particular column value from the row,but however in this case only the top row it will return.

Related

Spring Data JPA: How to update multiple column with different value

I am using Spring data jpa for creating services. I want to know about how to update multiple rows at one time with different values In spring data JPA. previously I updating record in table but only one row I updated.
I am not getting idea how to update multiple rows with different values on one save button click. How to receive parameters from front end
This is my table structure. I want to update percentage column. nRoomInvestigatorMappingId is varying may be next time I have 4 id with total percentage equals to 100(eg. 30,50,10,10 like this).
So Can any one tell me how to take input from front end and how to save?

How do I add onto an existing value in a column of mysql?

Is there a way to add values together to an existing column using hibernate statements? I have a web application that takes user input and adds points to their record in the database if the input is correct. So if correct, they get 50 points and that populates the points column in mysql, second time right should update the column to 100 and so on... The only ones I can find are insert and update but those just insert or replace, not add onto.
you return the object using (find) and you change the value of the attribute that you want like user.setRecord(user.getRecord() + 50) and you call the function (merge) of the entitymanager.

Hibernate query to fetch records taking much time

I am trying to retrieve a set of records from a table. The query I am using is:
select * from EmployeeUpdates eu where eu.updateid>0 and eu.department = 'EEE'
The table EmployeeUpdates has around 20 million records. 'updateid' is the primary key and there are no records currently in the table with the department 'EEE'. But the query is taking lots of time, due to which the web-service call is getting timed out.
Currently we have index only on the column 'updateid'. 'department' is a new column added for which we are expecting 'EEE' records.
What changes can I make to retrieve the results faster?
First off, your sql isn't valid, looks like you're missing an 'and' between the 2 conditions.
I'm guessing that all the update ID's are positive, and as its the primary key, they're unique, so I suspect eu.updateid>0 matches every row. This means it's not technically a Tablespace scan, but an index based scan, although if that scan then has all 20 million rows after matching the index, you might as well have a table space scan. The only thing you can really do is add an index to the department field. Depending on what this data is, you could have it on a seperate table, with a numeric primary key and then store that as a foreign key on the eu table. This would mean you scanned through all the departments, then got the updated associated to them, rather than searching every single update for a specific department.
I think you should look into using a Table-per-subclass mapping (more here: http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/inheritance.html#inheritance-tablepersubclass-discriminator). You can make the department the discriminator and then you'd have a EEEEmployeUpdates and ECEmployeeUpdates classes. Your query could change then to just query the EEEEmployeeUpdates.

how to create column in mysql table at runtime

In my web application I have a form in that I am creating dynamic select boxes and text boxes as per need(It can be more than 1), default there is one pair of select box and text box and my table has two columns for storing values of that select box and text box and values are getting store in my table , but problem is if I add more than one pair of select And text box in my form how to store values in my table? Could it be possible to create columns in table at RUN TIME as per need.
If you can count the number of text boxes and select boxes you can use sql's alter table query at runtime to add columns dynamically.
You created a bad schema for your application.
To simply solve this try schema like this:
groupid, selectbox, textbox
and store the data in more rows.
For example if there is 2 pairs of boxes then you store two rows:
1, 'select', 'text'
1, 'select2', 'text2'
And so on.
You can easily create by using the good old JDBC; you can see an example here. As suggested by others, this is not a proper design approach.
I do not know if it is possible by using JPA since the table creation is a property that you need to specify in persistence.xml . JPA can automatically create tables, but you will lose all data each time you want persist data to an existing table (unless there is some override configuration parameter which is unknown to me).

Determine if column in ResultSet contains values in all rows

In my application, I perform a costly query that takes minutes to produce a report. I am trying to make a generic class that transforms a ResultSet to and Excel spreadsheet, where a column is excluded from the spreadsheet if it only contains nulls. I can remove the columns from the Excel sheet after the fact easily, but it is difficult to "glue" worksheets back together after I have already split them when there are too many columns.
I could do a query to check if each column is null, but this would entail running the costly query all over again, perhaps multiple times, which would make the generation of the spreadsheet take too long.
Is there a way that I can query the ResultSet object that I already have (a little like ColdFusion) and remove columns from it?
EDIT
I ended up adding a pre-processing step where I added the column numbers of the used columns to a List<Integer> and then iterating through that collection rather than the set of all columns in the ResultSet. A few off-by-one errors later, and it works great.
Can you extract the data from the ResultSet and store it in memory first, before creating the work sheet, or is it too large? If so, then while you're extracting it you could remember whether a non-null value has been seen in each column. Once you're done extracting, you know exactly which columns can be omitted. Of course this doesn't work so well if the amount of data is so large that you wouldn't want to store it in memory.
Another solution would be to store the results of the costly query in a "results" table in the database. Each row for a given query execution would get stamped with a "query id" taken from a database sequence. Once the data is loaded into this table, subsequent queries to check whether "all values in column X are null" should be pretty speedy.
Note: if you're going to take this second approach, don't pull all the query data up to your application before storing it back to the results table. Rewrite the original "costly" query to do the insert. "insert into query_result(columns...) select {costly query}".
I could do a query to check if each
column is null
Better still you could incorporate that check into the original query, via a COUNT etc. This will be miles quicker than writing Java code to the same effect.

Categories