Select particular columns in Ebean - java

I want to select some particular fields from my database and send them to user as json but whenever i fetch data from database using ebean it selects all the columns
Optional<User>user= server.find(User.class).where().eq("name",username)
.and().eq("password", DigestUtils.sha1Hex(password))
.select("name").findOneOrEmpty();
if(user.isPresent())
return ok(Json.toJson(user));
It shows all fields of table in json but i want only name field back.

id column automatically in map when you select column.
Use setDistinct(true) before select column, it will only select name column
Optional<User>user = server.find(User.class).where().eq("name", username)
.and().eq("password",DigestUtils.sha1Hex(password)).setDistinct(true)
.select("name").findOneOrEmpty();
if(user.isPresent())
return ok(Json.toJson(user));

Related

How to include redundant column to show in SELECT Query from second table instead from the main table in Spring Repository?

I have a query like this:
Query 1:
select *
from items item
This is bind to an entity in SpringJPA.
Now I have to select one more column from another table like below.
Query 2:
select item.*,is.id
from items item
inner join item_state itm_s where item.id=itm_s.id
Now,there is a column "code" which exists in both item and item_state. I want to select it from item_state and not from item due to some data issues. I know the straight forward way is to write all columns in select statement,excluding code column from item and including code column from item_state.
But the thing is item table has around 100 columns.
Is there a good way to solve this issue without changing the entity class?
Look at Spring Data JPA projections and perform the following query:
#Query("select item.*, itm_s.code from items item join item.item_state itm_s where item.id = itm_s.id")
You will have to look at the resulting query to write your projection class.

Relationship does not exist in SQL

I sent a command to the database
INSERT INTO UserEntity(id, username, email, password, enabled, registration_date, modified_date)
SELECT
1, 'JonkiPro', 'someemail#someemail.com,', 'safsd', true, GetDate(), GetDate()
WHERE NOT EXISTS (
SELECT * FROM users WHERE username = 'JonkiPro'
);
which is to add a new user to the database. Cherry during compilation throws out the error
ERROR: relationship "userentity" does not exist
I am in the application created entity 'UserEntity' which represents the user with an annotation added #Table(name="users")?
How do I make a mistake when creating this query?
Pretty clear your error code. Basically you are trying to insert some data in a table userentity that doesn't exist in your database schema. (In SQL, tables = relationships).
You just have to put the name of your database table that you want to insert into or create a table named userentity.

Saving in a web application

I have a web application (using Vaadin, Hibernate, and Postgres database) in which users can add data to a certain table. When the user adds an item to this table they are asked to enter the name, date, and selected from a table all the related products. When they click save I am able to save the name and date into the database but am having trouble grabbing the items from the table and putting them into a set to add. The bean class for the table looks like this:
/** Created by Hibernate **/
public class BeanClass implements Serializable {
private String name;
private Date date;
private Set relatedProducts = new HashSet(0);
.
.
.
}
The code I am using to save the item the user wants to add is:
BeanClass toAdd = new BeanClass();
Set temp = null
/** There is a table where the user can select all the products they want to add
* When they select an item it goes to another table, so what I am doing here
* is adding looping through latter table and adding all the items they selected
* to a set (supposedly i think this should work
**/
for (Object item : table) {
temp.add(table.getContainerProperty("id", item));
}
toAdd.setName(nameField.getValue()); //I have input fields for the name and date
toAdd.setDate(dateField.getValue());
toAdd.setRelatedProducts(temp);
hbsession.save(toAdd);
When the user clicks save the name and date is added to the table, but the relatedProducts is not added to the relationship table (in the database). I have looked into cascade and inverse in hibernate and am thinking that I will have to use these but do not quite understand how.
what datatype is your relatedProducts Set? is it another Bean, then you have to define a OneToMany annotation.
did you debug if the Set is filled before writing it to the database?
You dont really need to iterate over the whole table to get selected values. You can get the items selected via table.getValue()
getValue()
Gets the selected item id or in multiselect mode a set of selected ids.
If it's the same datatype in the table as it is in your Bean, then
toAdd.setRelatedProducts(table.getValue());
should be enough.

Sql Injection : want to show a demo of sql injection

I want to show a attack of sql injection in which when user fill a user login form and submit to java servlet. in login and password filed how we are type a query which is update any other record. I know all column and table names.
for example I am write this query in servlet :
select userid,username from accountinfo where userid='testid' and pass='1234';
update accountinfo set emailid='aar#r.com' where userid='testid2';
but its give Sql Exception how to solve this issue.
Try a single query first:
select userid, username
from accountinfo
where userid='-' and pass='-'
union
select userid, pass
from accountinfo
where userid like 'adm%'
If that gives no exception, present first the query of system tables, and then the above query. Pick an injection of an SQL update for the update accountinfo.

retrieve database into JTextField

i'm doing a project where i need to retrieve data from one field of access and display it on JTextField and its serial no.select fieldname from tablename
but it'l select all rows.also how to retrieve data(whole row) with random serial number
For the query to select the field data,
SELECT fieldName FROM tableName WHERE columnName = 'columnValue';
Here, columnName should be something like the primary key and columnValue will be a unique value (like a serial number). Then it will return the field data associated with the row with the specific columnValue.
For setting it into the JTextField,
if(resultSet.first()) {
jTextField.setText(resultSet.getString(0));
}
Here, resultSet should return only one value (with the query specified above).
As for your other question, if your serials are irregular, get all the serials from the table:
SELECT serials FROM yourTable;
And then add them to a Collection (like a Vector). Then write an algorithm to generate a random index and retrieve the data from the Collection with that index (vector.elementAt([yourIndex])).
Then again query the table in the database for the row associated with that serial:
SELECT FROM yourTable WHERE serialNo = 'theValueYouGot';
...and you'll get the relevant row.

Categories