How to use auto generated ID in spring hibernate Model class - spring

I have model class as below in my entity class
#Entity
#Table(name="TblKnow")
public class Know {
#Id
#Column(name="IdKnow")
private Double IdKnow;
The ID is autogenerated by database, so i do not need to fill the ID.
But when i put the value id as null i get below exception
:ids for this class must be manually assigned before calling save(): com.melk.spring.model.Know

First set your id type to Long, then add #GeneratedValue(strategy=GenerationType.IDENTITY), don't forget to add a getter & setter for this field.
#Id
#Column(name="IdKnow")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long IdKnow;
Make sure that on your DB the id column is of type BIGINT

Related

Javax persistence id column and oracle generated always

It should be easy. Oracle has defined "generate always" for an id column. So there is no need to add this id to the insert statement. How do i define my entity that it is handling right.
#Entity
public class Example {
#Id
#GeneratedValue // if i remove this annotation than hibernate complains that i need to set it manually
#Column(name="ID", insertable=false, updatable=false)
private Long id;
}
in the error message i see that it still tries to add the id to the query.
My work-around for the column. Add the #id to an other column and remove the id column from entity definition :-D

Save only one field of an entity to another table

How do I only save a certain field/property of an Entity for another entity?
I have a Key entity with id and name properties.
In my Door entity, I would like to accept a Key but only save in the database the id
Is that possible?
Have a look at the #Transient annotation. Example from the docs:
#Entity
public class Employee {
#Id int id;
#Transient User currentUser;
...
}
#Transient will mark the annotated element to not be persisted.

Advantage of assigning the returned savedEntity in Spring Data

I see in most of the coders save data(using spring data) as:
savedEntity = repo.save(savedEntity);
Long id = savedEntity.getId();
I am confused about why most of them assign back the returned value to the saved Entity while the following code also works exact(I have tested myself):
repo.save(savedEntity);
Long id = savedEntity.getId();
Did I miss some benefit of assigning back?
for example, let the entity be:
#Entity
public class SavedEntity {
#Id
private int id;
private String name;
//getter, setters, all arg-constructor, and no-arg constructor
}
Consider the object of SavedEntity is
SavedEntity entity = new SavedEntity(1,"abcd");
now for your first question,
SavedUser entity1 = repo.save(entity);
Long id = entity1.getId();
this entity1 object is the return object getting from the database, which means the above entity is saved in the database succesfully.
for the Second Question,
repo.save(entity);
Long id = entity.getId();//which you got it from SavedEntity entity = new SavedEntity(1,"abcd");
here the value of id is the integer you mentioned in place of id(the raw value).
Most of the time the id (primary key) is generated automatically while storing the entity to the database using strategies like AUTO, Sequence etc. So as to fetch those id's or autogenerated primary key values we assign back the saved entity.
For example:
#Entity
public class Customer {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
}
In this case you'll not pass the id externally but it will create a value for it automatically while storing the data to DB.

#Id annotation Causing duplication in list

I am using Hibernate for calling Stored procedure
Response returned by Stored procedure
receiverId fcmId source
1234 xyz android
45678 abc web
9876 fgh android
1234 ygh ios
Hibernet #EntityClass
#Entity
public class receieverDetails {
#Id
#Column(name="receiverId")
private String receiverUserId;
#Column(name="fcmId")
private String fcmIds;
private String source;
}
I am getting List of receiverDetails from database
if List contain duplicate receiverId as show is above response, 1st one is replacing the 4th details
Code for Binding
ProcedureCall procedureCall1 =
session.createStoredProcedureCall(Strings.StoredProcedureNames.GET_RECEIVER_INFO_OF_SPONSORED_MESSAGE,receieverDetails.class);
Output output1 = procedureCall1.getOutputs().getCurrent();
if(output1.isResultSet()) {
List<receieverDetails> receievers = ((ResultSetOutput) output1).getResultList();
}
i think this is causing by #Id annotation in the entity class, Because it is happening with same receiverIds only
Kindly Help me on this
In your code by providing the #Id annotation to the column receiverId, you are telling the code that this field is to be used as the primary key for the table.So, when fetching the data the issue occurs as there are duplicate values in the table for this column. Either you need to set the primary key correctly, or make this column as primary key in table and correct your code.
If you are using the same entity class to persist data and make column receiverId primary key then try using the below :
#Entity
public class receieverDetails {
#Id
#Column(name="receiverId",unique=true,nullable = false)
#GeneratedValue(strategy = GenerationType.AUTO)
private String receiverUserId;
#Column(name="fcmId")
private String fcmIds;
private String source;
}
unique=true in #Column is a shortcut for #UniqueConstraint(columnNames = {"receiverId"} and other particular constraints.The #GeneratedValue annotation is to configure the way of increment of the specified column(field).
or if the primary key of the table is some other field in table please correct the code to reflect the same.

Spring Data Redis -- How to have alias name for the variable in spring data redis?

In the below code if I want to store the id as student_id and name as student_name in redis database and I don't want to change the name of variable in my java code. Is there any way out to provide alias name for the variable as we do in spring data jpa we specify the name of the column in #Column annotation. Please help me out if anyone has the solution for the same.
#RedisHash("Student")
public class Student implements Serializable
{
#Id
private Long id;
private String name;
private float marksObtained;
private String schoolName;
}

Resources