Problem with saveOrUpdate() in hibernate - java

Recently i am started using hibernate. to insert or update i am using saveOrUpdate() function.
If i update an entry i works fine. But, with new entry hibernate generate a Query. Buts nothing gets updated in the table.
My scenario is like this,
i am using Many to One & One to Many relationship between two tables[Expense, Category]
For update or insert, i am creating two objects(Expense expense, Category category) from the
client side. In the server side, i set category object with expense object.
public void upDateExpenseTable(Expens expense, Category category) {
expense.setCategory(category);
Session session = Main.getSession();
try{
System.out.println("Inside Update Try Block");
session = Main.getSession();
Transaction tr = session.beginTransaction();
session.saveOrUpdate(expense);
tr.commit();
}
finally{
session.close();
}
}
Object structure is like, catogery.catId, category.catName &
expense.expnsId, expense.expnsName, expense.amount, expense.status, expense.userName.
But there is another one column in Expense Table cat_id. Its is through mapping annotation. But i dont have any property for that in Expense entity.
When inserting new data, i am not giving any Id.
Any suggestions!!!
public class Expens implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int expnsId;
private int amount;
private String date;
private String status;
private String userName;
#ManyToOne
#JoinColumn(name = "cat_id")
private Category category;
//Setter Getter
}
Category Classs
public class Category{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int catId;
private String catName;
#OneToMany(targetEntity=Expens.class, mappedBy = "category", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private List<Expens> expenses;
}//Setters Getters ommited.

I'm not sure I understood everything but I can at least tell you that you're not building your bi-directional association correctly, you need to set "both sides of the link" when building your object graph:
...
expense.setCategory(category);
category.getExpenses().add(expense);
...
session.saveOrUpdate(category);
And this is usually done in "link management" methods, like this (in Category):
#Entity
public class Category {
#Id #GeneratedValue(strategy=GenerationType.AUTO)
private int catId;
private String catName;
#OneToMany(targetEntity=Expens.class, mappedBy = "category", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private List<Expens> expenses = new ArrayList<Expens>();
public void addToExpenses(Expense expense) {
expenses.add(expense);
expense.setCategory(this);
}
protected List getExpenses() { // protected to prevent direct access from outside the hierarchy
return expenses;
}
protected void setExpenses(List expenses) { // protected to prevent direct access
this.expenses = expenses;
}
//...
}
And the code becomes:
category.addToExpenses(expense);
session.saveOrUpdate(category);
Funnily enough (or not), I've written about this three times today.
Resource
Hibernate Core Documentation
1.2.6. Working bi-directional links

Related

Stuck in a loop while passing RequestBody in Postman

#Entity
#Table(name = "customers")
public class Customer implements Serializable{
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private int custID;
private String custName;
#Id
private String email;
private int phone;
#OneToMany (mappedBy = "customer", fetch = FetchType.LAZY)
private List<Transaction> transaction;
#Entity
#Table(name = "transactions")
public class Transaction implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int transID;
private Date date;
private int amount;
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "custID", nullable= false)
private Customer customer;
These are my entities, and I have a method:
#PostMapping("/record-transaction")
public Transaction recordTransaction(#RequestBody Transaction transaction) {
return transactionService.addTransaction(transaction);
}
But when I try to create JSON in postman, I get into a loop where while entering values for transaction, at the end I must enter the Customer object as well and when I am entering customer object at the end I again reach to enter the transaction's values. Its like a never ending loop. Help
I couldn't think of anything to do at all. My mind enters the loop itself.
Decouple your DB entities from your request/response by using an intermediate DTO.
Controller:
#PostMapping("/record-transaction")
public TransactionResponse recordTransaction(#RequestBody TransactionRequest body) {
return TransactionResponse.from(transactionService.addTransaction(
body.getDate();
body.getAmount();
body.getCustomerId();
));
}
TransactionRequest:
public class TransactionRequest {
//don't need ID here it'll be auto generated in entity
private Date date;
private int amount;
private int customerId;
}
TransactionResponse:
public class TransactionResponse {
private int id;
private Date date;
private int amount;
private int customerId;
public static TransactionResponse from(Transaction entity) {
return //build response from entity here
}
}
TransactionService:
//when your entity is lean may as well pass the values directly to reduce boilerplate, otherwise use a DTO
public Transaction addTransaction(Date date, int amount, int customerId) {
Customer customerRepo = customerRepo.findById(customerId).orElseThrow(
() -> new CustomerNotFoundException();
);
Transaction trans = new Transaction();
trans.setDate(date);
trans.setAmount(amount);
trans.setCustomer(customer);
return transactionRepository.save(trans);
}
If you want to embed the customer model inside TransactionResponse or TransactionRequest it'll be fairly easy to do and this solution will produce way nicer contract and swagger docs than a bunch of use case specific annotations in your entity.
In general decoupling you request/response payloads, service dtos and entities from each other results in code with more boilerplate but easier to maintain and without weird unexpected side effects and specific logic.

How to send ArrayList or Sets<Integer> as list filtering in Request Body or URL in PUT Request

I'm trying to write a controller that will function as multiple seat reservation.The Integers list is used for filtering.
My Entity looks like this:
#Entity
#Id
private movieId;
private String movieName;
private String cinemaName;
private String cinemaHall;
private Intger seatingPlace;
private boolean booked;
How Can I pass list or sets in request body to access multiple update seatingPlace. Did I modyfing Enity or connect in smthing relation?
Acutally my multipleUpdate API works using JPA Query findByMovieNameAndCinemaNameAndcinemaHall and return me list wchich
I checking isnt Empty and cheking (field boolean booked) if true so ok u can booked them.
And after that i want filter by passing List seatingPlace and change boolen to false.
Based on my understanding of your requirements, a possible solution could be creating another entity (table) MovieSeatReservation and creating a One to Many relationship from your Entity. It could look like this: (You can replace Entity class name with your real entity name)
#Entity
public class Entity {
#Id
#GeneratedValue
private Long movieId;
private String movieName;
private String cinemaName;
private String cinemaHall;
#OneToMany
private List<MovieSeatReservation> reservedSeatsStatus;
// getters and setters
}
#Entity
public class MovieSeatReservation {
#Id
#GeneratedValue
private Long id;
private boolean isReserved;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "movie_id")
private Entity entity;
// getters and setters
}

bidirectional onetomany on spring boot and hibernate, best way to save

I have 2 entities:
#Data
#Entity
#EqualsAndHashCode(onlyExplicitlyIncluded = true)
#Table(name = "source_company")
public class SourceCompany {
#Id
#EqualsAndHashCode.Include
private UUID id;
private String name;
#OneToMany( mappedBy = "company")
private final Set<SourceUser> users = new HashSet<>();
#Column(name = "version")
#Version
private Long version;
}
#Data
#Entity
#EqualsAndHashCode(onlyExplicitlyIncluded = true)
#Table(name = "source_user")
public class SourceUser {
#Id
#EqualsAndHashCode.Include
private UUID id;
private String name;
#Column(name = "version")
#Version
private Long version;
//ref
#ManyToOne
#JoinColumn(name = "fk_source_company")
private SourceCompany company;
}
Is it correct to save in this way (only 2 save)?
#Test
public void testSourceUserSave() {
SourceCompany sourceCompany= new SourceCompany();
sourceCompany.setName("xxx");
sourceCompany.setId(UUID.fromString("2bf05cbc-d530-11eb-b8bc-0242ac130003"));
SourceUser sourceUser= new SourceUser();
sourceUser.setName("dev-team");
sourceUser.setId(UUID.fromString("4bede7a0-d530-11eb-b8bc-0242ac130003"));
sourceUser.setCompany(sourceCompany);
sourceCompany.getUsers().add(sourceUser);
sourceCompanyRepository.save(sourceCompany);
sourceUserRepository.save(sourceUser);
assertNotNull(sourceUser);
assertEquals(sourceUser.getCompany().getId(), sourceCompany.getId());
assertEquals(sourceCompany.getUsers().stream().findFirst().get().getId(), sourceUser.getId());
}
or I need to save the user (without company) and the company (without user) and after that to update the user with a save and the company (without save because is not the owner) like this (3 save):
#Test
public void testSourceUserSave() {
SourceCompany sourceCompany= new SourceCompany();
sourceCompany.setName("xxx");
sourceCompany.setId(UUID.fromString("2bf05cbc-d530-11eb-b8bc-0242ac130003"));
SourceUser sourceUser= new SourceUser();
sourceUser.setName("dev-team");
sourceUser.setId(UUID.fromString("4bede7a0-d530-11eb-b8bc-0242ac130003"));
sourceUserRepository.save(sourceUser);
sourceCompanyRepository.save(sourceCompany);
sourceUser.setCompany(sourceCompany);
sourceCompany.getUsers().add(sourceUser);
sourceUserRepository.save(sourceUser);
assertNotNull(sourceUser);
assertEquals(sourceUser.getCompany().getId(), sourceCompany.getId());
assertEquals(sourceCompany.getUsers().stream().findFirst().get().getId(), sourceUser.getId());
}
It seems, looking in the db, that the first way works, so in future can I update only the owner side (I mean update and save) and so can I update the not-owner side only in the object without save it again?
Thanks in advance
You usually tend to save only one of the objects. This can be done adding the
#ManyToOne(cascade = CascadeType.PERSIST)
to the mapping annotation. This makes sure that the nested entities get persisted too
You would need to do just:
SourceCompany sourceCompany= new SourceCompany();
sourceCompany.setName("xxx");
sourceCompany.setId(UUID.fromString("2bf05cbc-d530-11eb-b8bc-0242ac130003"));
SourceUser sourceUser= new SourceUser();
sourceUser.setName("dev-team");
sourceUser.setId(UUID.fromString("4bede7a0-d530-11eb-b8bc-0242ac130003"));
sourceUser.setCompany(sourceCompany);
sourceUserRepository.save(sourceUser);
One more thing to note is that the .save method actually returns an entity itself. That entity is the persisted entity just created. Basically if you manage everything within a single transactional method any modification to the persisted entity within that method (transaction) will be applied without calling any save, merge or update method
I suggest reading about the #Transactional annotation

Why is Entitys mapped field NULL after fetch even it is persisted with CascadeType.ALL?

I have a business in which I have agencies and backlogs. One Agency has one backlog.
I have a query that allow me to retrieve all agencies, but I get an NPE when trying to access backlog of the agencies retrieved.
When I get a saved object in JPA, shouldn't I have the associated objects as well ?
Here are my entities and service:
Agency
#NamedQuery(name="allAgences", query="select a from Agency a")
#Stateless
#Entity
public class Agency implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue
private int id;
private String name;
#OneToOne(optional = true, orphanRemoval = true,
cascade=CascadeType.ALL, mappedBy="agency",
targetEntity=Backlog.class )
private Backlog backlog;
public Agency(String name, Backlog backlog) {
this.name = name;
this.backlog = backlog;
}
public Agency() {
this.setName("");
this.setBacklog(new Backlog());
}
}
// getters, setters...
Backlog
#Stateless
#Entity
public class Backlog implements Serializable {
private static final long serialVersionUID = 1L;
Id
#GeneratedValue
private int id;
#OneToOne(optional = true)
#JoinColumn(name = "agency_id")
private Agency agency;
public Backlog() {}
//getters, setters....
}
My Ejb Service
#Stateless
#LocalBean
public class AgencyBean implements AgencyBeanRemote {
#PersistenceContext
private EntityManager em;
/**
* Default constructor.
*/
public AgencyBean() {}
#Override
public Agency createAgency(String agencyName) {
Agency agency = new Agency();
agency.setBacklog(new Backlog());
agency.getBacklog().setEntries(new ArrayList<>());
agency.setName(agencyName);
em.getTransaction().begin();
em.persist(agency);
em.getTransaction().commit();
return agency;
}
#Override
public List<Agency> getAllAgencies() {
Query q = em.createNamedQuery("allAgences");
List<Agency> agencies = q.getResultList();
for(Agency agency : agencies) {
// System.out.println("hello " + agency.getBacklog().getId());
// give me a NPE
}
return agencies;
}
}
I guess that the NPE is because backlog is null in agency.getBacklog().getId()
If you take a look on the database and table backlog I think you see that agency_id is null. Therefore Backlogs cannot be attached to Agency.
To fix this change - in your public createAgency(String agencyName)- row
agency.setBacklog(new Backlog());
to rows
Backlog backlog = new Backlog();
agency.setBacklog(backlog);
backlog.setAgency(agency); // this should do the fix
And just to mention - namely if I guessed the NPE reason correct - you have
#OneToOne(optional = true ...
private Backlog backlog;
so you should expect backlog also to be null but you do not check that in your code? (Yes I realize that it is possibly only for a test purposes but just to to mention)
Related: Persist OneToOne relation
Update
In Entity Agency you can also implement following
#PrePersist
private void prePersist() {
// add needed NPE checks etc...
backlog.setAgency(this);
}
This might reduce boilerplate code since there is no more need to add this backlog.setAgency(agency) everywhere Agency is created the way you do it.

PersistenceException, Column 'id' specified twice

I have the following files in Play Framework 2.2.3
Controller:
public class Comment extends Controller
{
public Result create(UUID id)
{
models.blog.Blog blog = models.blog.Blog.finder.byId(id);
Result result;
if(blog == null)
{
result = notFound(main.render("404", error404.render()));
}
else
{
Form<models.blog.Comment> commentForm = Form.form(models.blog.Comment.class);
commentForm = commentForm.bindFromRequest();
if(commentForm.hasErrors())
{
result = badRequest(Json.toJson(commentForm));
}
else
{
models.blog.Comment comment = commentForm.get();
comment.setId(UUID.randomUUID());
comment.setTimeCreated(new Date());
comment.setBlogId(blog.getId());
comment.save();
result = ok(Json.toJson(comment));
}
}
return result;
}
}
And two models
#Entity
#Table(name="blog")
public class Blog extends Model
{
private static final SimpleDateFormat MONTH_LITERAL = new SimpleDateFormat("MMMMM"),
DAY_NUMBER = new SimpleDateFormat("d"),
YEAR_NUMBER = new SimpleDateFormat("yyyy");
public static Finder<UUID, Blog> finder = new Finder<UUID, Blog>(UUID.class, Blog.class);
#Id
#Column(name="id",length=36, nullable=false)
public UUID id;
#OneToOne
#JoinColumn(name="author_id")
public User author;
#Column(name="title",length=255)
public String title;
#Column(name="summary",length=255)
public String summary;
#Column(name="url",length=255)
public String url;
#Column(name="content")
public String content;
#Column(name="time_updated")
public Date time_created;
#Column(name="time_created", nullable=false)
public Date time_updated;
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name="blog_id")
public List<Comment> comments;
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(
name="blog_tag_map",
joinColumns={ #JoinColumn(name="blog_id", referencedColumnName="id") },
inverseJoinColumns={ #JoinColumn(name="tag_id", referencedColumnName="id") }
)
public List<Tag> tags;
public List<Comment> getComments()
{
return this.comments;
}
}
#Entity
#Table(name="blog_comment")
public class Comment extends Model
{
private static final SimpleDateFormat MONTH_LITERAL = new SimpleDateFormat("MMMMM"),
DAY_NUMBER = new SimpleDateFormat("d"),
YEAR_NUMBER = new SimpleDateFormat("yyyy");
#Id
#Column(name="id",length=36, nullable=false)
public UUID id;
#Column(name="blog_id", length=36)
public UUID blog_id;
#ManyToOne
public Blog blog;
#Column(name="content", length=500)
public String content;
#Column(name="website", length=255)
public String website;
#Column(name="name", length=255)
public String name;
#Column(name="time_created", updatable=false)
public Date time_created;
}
I have excluded some setters and getters from these models for brevity, so it doesn't clog up this post.
When I attempt to make a POST request to the aforementioned controller, everything goes fine until I get to the "comment.save()" statement in the controller file, then I get the following error.
I'm unsure why this save isn't going through, and why there is a column conflict.
Help much appreciated
The issue lies in the fact that you have defined basically two foreign key columns for Blog in your Comment's entity:
#Column(name = "blog_id", length = 36)
public UUID blog_id;
#ManyToOne
public Blog blog;
The default column name for your 'blog' field is: blog_id
However, you've already named your 'blog_id' column that.
Interestingly, no error/warning is thrown when creating this table...
So when you call comment.save(), the following insert statement is generated:
insert into blog_comment (id, blog_id, content, website, name, time_created, blog_id) values (?,?,?,?,?,?,?)
Notice a reference to 'blog_id' column twice, which is invalid.
And this is because of the above double mapping.
To fix, just give your 'blog' property a different name to use for the foreign key column:
#Column(name = "blog_id", length = 36)
public UUID blog_id;
#ManyToOne
#JoinColumn(name = "blogId")
public Blog blog;
I'm not sure why you're mapping your entities like this (perhaps legacy schema?) but the 'blog_id' fields seem to be redundant (and confusing) as you already have an entity mapping in the form of your 'blog' property.
This question is pretty old, but for any future reference i have found this answer that solved my problem.
After numerous searchers around the web I found this answer here - thanks to jtal!
Just to summaries the problem:
Using Ebean i have made a #ManyToOne entity that is not implemented in the database in anyway,
even more the join field, in your case
blogId
is a valid field that has values of its own.
when trying to join the column on that field, it will always fail because it creates this sql query:
SELECT
*
FROM
blog_comment;
select
t0.id c0,
t0.blog_id c1,
t0.content c2,
t0.website c3,
t0.time_created c4,
t0.blog_id c5 <---- notice this duplicate
from
blog_comment t0
in order to solve this, i tell ebean not to use the second set of properties.
your new ebean element should look something like this:
#ManyToOne
#JoinColumn(name = "blogId", insertable = false, updatable = false)
public Blog blog;
hope this helps! =)

Categories

Resources