How to save multiple objects via ArrayList in hibernate? - java

I have a Student entity. My idea is to collect multiple student objects in an ArrayList and save all objects from that list to the database. When do you use #ElementCollection annotation? Does it apply to situations like this?
Student:
package basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity
public class Student {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
public Student(String name) {
this.name = name;
}
}
Runner:
package basic;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Runner {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure("/basic/hibernate.cfg.xml").buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
List<Student> students = new ArrayList<>();
students.add(new Student("Michael"));
students.add(new Student("Dave"));
students.add(new Student("Tom"));
students.add(new Student("Dinesh"));
students.add(new Student("Lakshman"));
students.add(new Student("Cruise"));
session.save(students);
session.getTransaction().commit();
session.close();
}
}
Error
Exception in thread "main" org.hibernate.MappingException: Unknown entity: java.util.ArrayList
at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1596)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:668)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:660)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:655)
at basic.Runner.main(Runner.java:27)

You have to do something like this:
for(Student student : students) {
session.save(student);
}
If you want to save entity you should map it. ArrayList<> is not mapped entity. Student has mapping so you should save it separately.

#ElementCollection you should use to define relation between object - here you have nice explenation https://en.wikibooks.org/wiki/Java_Persistence/ElementCollection
To save list of object, you need to iterate by objects, something like this -> How to insert multiple rows into database using hibernate?

I would further recommend to use another Hibernate command to avoid an Out Of Memory error...
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
for (int i = 0 ; i < students.size(); i++) {
session.save(students.get(i));
if (i % 100 == 0) {//a batch size for safety
session.flush();
session.clear();
}
}
transaction.commit();
session.close();
sessionFactory.close();

Related

Hibernate mapping exception even though mappings appear correct

I've this Java application in package com.luv2code.hibernate.demo:
package com.luv2code.hibernate.demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.luv2code.hibernate.demo.entity.Employee;
public class CreateEmployeeDemo {
public static void main(String[] args) {
// create session factory
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Employee.class)
.buildSessionFactory();
// create a session
Session session = factory.getCurrentSession();
try {
// create the employee object
System.out.println("Creating a new employee object...");
Employee tempEmployee = new Employee("John", "Doe", "Doe Corp.");
// start a transaction
session.beginTransaction();
// save the employee object
System.out.println("Saving the employee...");
session.save(tempEmployee);
// commit the transaction
session.getTransaction().commit();
// find the employee's ID: primary key
System.out.println("Save employee. Generated ID: " + tempEmployee.getId());
// now get a new session and start transaction
session = factory.getCurrentSession();
session.beginTransaction();
// retrieve employee based on the ID: primary key
System.out.println("\nGettin employee with id: " + tempEmployee.getId());
Employee myEmployee = session.get(Employee.class, tempEmployee.getId());
System.out.println("Get complete: " + myEmployee);
// commit the transaction
session.getTransaction().commit();
// delete student id=2
System.out.println("Deleting student id=2");
session.createQuery("delete from Student where id = '2'").executeUpdate();
// commit the transaction
session.getTransaction().commit();
System.out.println("Done!");
}
finally {
factory.close();
}
}
}
It uses this Employee class in package com.luv2code.hibernate.demo.entity:
package com.luv2code.hibernate.demo.entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
public class Employee {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="id")
private int id;
#Column(name="first_name")
private String firstName;
#Column(name="last_name")
private String lastName;
#Column(name="company")
private String company;
public Employee() {
}
public Employee(String firstName, String lastName, String company) {
this.firstName = firstName;
this.lastName = lastName;
this.company = company;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
#Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", company=" + company + "]";
}
}
When I run the program I get this error:
Saving the employee...
Jul 17, 2020 6:46:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PoolState stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false&serverTimezone=UTC]
Jul 17, 2020 6:46:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections close
ERROR: Connection leak detected: there are 1 unclosed connections upon shutting down pool jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false&serverTimezone=UTC
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.luv2code.hibernate.demo.entity.Employee
at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:704)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1606)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:179)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:75)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:634)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:627)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:622)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:351)
at com.sun.proxy.$Proxy25.save(Unknown Source)
at com.luv2code.hibernate.demo.CreateEmployeeDemo.main(CreateEmployeeDemo.java:33)
I can't see what could be wrong here. It seems to be a mapping exception but I think my mappings should be correct. Can anybody spot the issue?
Use #Entity and #Table for map database table name with Employee class
#Entity
#Table(name = "employee")
public class Employee { ... }

TypeMisMatchException in hibernate

Following is my Person class:
package com.subir.sample;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
#Entity
#Table(name ="person",uniqueConstraints = {#UniqueConstraint(columnNames= {"NAME"})})
public class Person implements Serializable{
/**
*
*/
private static final long serialVersionUID = -2728179031744032393L;
int age;
String name;
char isVip;
public Person() {
}
public Person(int age, String name, char isVip) {
this.age = age;
this.name = name;
this.isVip = isVip;
}
#Id
#Column(name="AGE")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
#Id
#Column(name="NAME")
//#OneToMany(mappedBy="NAME")
private Set <Subject> subjects;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Id
#Column(name="ISVIP")
public char getIsVip() {
return isVip;
}
public void setIsVip(char isVip) {
this.isVip = isVip;
}
}
And the following is my class for add and view persons.
package com.subir.sample;
import org.hibernate.Transaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class PersonDbAccess {
public static void addPerson(String name, int age, char isVip, Session session, org.hibernate.Transaction tx) {
try {
tx = session.beginTransaction();
Person p = new Person(age, name, isVip);
session.save(p);
tx.commit();
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public static void viewPerson(String name, Session session) {
try {
System.out.println("Name value in view method is :: " + name);
Person person = (Person)session.get(Person.class, name);
System.out.println("Person.class is ::" + Person.class + " Person.class.getName is :: "
+ Person.class.getName() + " Person.class.getSimpleName() is :: " + Person.class.getSimpleName()
+ " Person.class.getCanonicalName is :: " + Person.class.getCanonicalName());
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
public static void main(String[] args) {
SessionFactory factory = HibernateUtils.buildSessionFactory();
Session session = factory.openSession();
Transaction tx = null;
String name = "Subir";
int age = 20;
char isVip = 'Y';
// addPerson(name,age,isVip,session,tx);
viewPerson("Subir", session);
}
}
And following is my stacktrace:
Exception in thread "main" org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.subir.sample.Person. Expected: class com.subir.sample.Person, got class java.lang.String
at org.hibernate.event.internal.DefaultLoadEventListener.checkIdClass(DefaultLoadEventListener.java:166)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1240)
at org.hibernate.internal.SessionImpl.access$1900(SessionImpl.java:204)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.doLoad(SessionImpl.java:2842)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2816)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:1076)
at com.subir.sample.PersonDbAccess.main(PersonDbAccess.java:53)
Even though, I am passing object.class,String in the get method of the session object, it is giving me type mismatch exception.
You need to pass the ID to your hibernate session get method. In your case you have a compound key , fetching only by name might result in more than one object being returned which contradicts with the nature of the get operation which returns a single object by primary key.
You need to use a Criteria or Query here.
On another note if you want to use the Session.get method, instead of marking multiple columns with ID which is not JPA complient you should create an EmbededId and use it in the Session.get method. Then it will not incompatible type.
Read https://vladmihalcea.com/the-best-way-to-map-a-composite-primary-key-with-jpa-and-hibernate/
UPDATE:
I can see you have Unique constraint on name. Why do you need the AGE and the isVip to be part of your key , because marking them with ID you are effectivly making them part of your key.

Unable to update a record via Hibernate

I am able to successfully create and insert entries in a table via Hibernate, however for some reason my update method appears to not be working.
For my table, I chose to use Java annotations in the POJO file to create it.
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* #author
*/
#Entity
#Table(name="student") //name of DB table that will be created via Hibernate
public class Student {
#Id //Primary Key
#Column(name = "id") //map to column
private Integer id;
#Column(name = "name")
private String name;
#Column(name = "marks")
private Integer marks;
public Student(Integer id, String name, Integer marks) {
this.id = id;
this.name = name;
this.marks = marks;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getMarks(){
return marks;
}
public void setMarks(Integer marks) {
this.marks = marks;
}
#Override
public String toString() {
return "Student: " + this.getId() + " | " + this.getName() + " | " + this.getMarks();
}
}
As aforementioned, the table is successfully created in a MySQL database. However, I am unable to update an objects Marks (grade) via my HQL query:
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
*
* #author
*/
public class HibernateModuleTen {
private static SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class)
.buildSessionFactory();
public static void main(String[] args) {
Session newSession = factory.getCurrentSession();
try {
/*Create Student Objects
in Memory
*/
Student student1 = new Student(100, "Greg", 95);
Student student2 = new Student(101, "Mary", 91);
Student student3 = new Student(102, "Sidi", 90);
Student student4 = new Student(103, "Rokia", 92);
Student student5 = new Student(104, "Abdel", 88);
Student student6 = new Student(105, "Christine", 77);
Student student7 = new Student(106, "Hamma", 90);
Student student8 = new Student(107, "Ahmadu", 68);
Student student9 = new Student(108, "Halimatu", 96);
Student student10 = new Student(109, "Iziren", 99);
//Begin transaction
newSession.beginTransaction();
//Save all the students
newSession.save(student1);
newSession.save(student2);
newSession.save(student3);
newSession.save(student4);
newSession.save(student5);
newSession.save(student6);
newSession.save(student7);
newSession.save(student8);
newSession.save(student9);
newSession.save(student10);
newSession.getTransaction().commit();
//Update a Student Record
updateStudent(107, 34);
//Delete a record if marks are less than 35 and then update Database
deleteStudent();
//Print all records
newSession = factory.openSession();
newSession.beginTransaction();
Criteria newCriteria = newSession.createCriteria(Student.class);
List<Student> students = newSession.createQuery("from Student").list(); //.list is .getResultList in later versions of Hibernate
for (Student aStudent : students) {
System.out.println(aStudent.toString());
}
newSession.getTransaction().commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
factory.close();
}
}
public static void updateStudent(Integer id, Integer marks) throws HibernateException {
/*Update Transaction*/
Session newSession = factory.openSession();
newSession.beginTransaction();
Student studentToUpdate = (Student)newSession.get(Student.class, id); //Choose record 107 to update
//Update the marks of Student based on ID and marks
studentToUpdate.setMarks(marks);
newSession.update(studentToUpdate);
//Commit to the Transaction
newSession.getTransaction().commit();
newSession.close();
}
public static void deleteStudent() throws HibernateException {
Session newSession = factory.openSession();
newSession.beginTransaction();
newSession.createQuery("delete from student s where smarks < 35")
.executeUpdate(); //Used for updates and deletes
newSession.getTransaction().commit();
newSession.close();
}
}
The update method effectively takes one of the records in the table via the id column and updates the element in the marks column.
There was an issue with my delete method:
public static void deleteStudent() throws HibernateException {
Session newSession = factory.openSession();
newSession.beginTransaction();
newSession.createQuery("delete from student s where smarks < 35")
.executeUpdate(); //Used for updates and deletes
newSession.getTransaction().commit();
newSession.close();
}
If one looks closely at the query, the "delete from student" should be "delete from Student" with a capital s. Careless error.
You didn't post your errors here, but looks like your Student bean class doesn't have a default constructor which is being invoked while executing
Student studentToUpdate = (Student)newSession.get(Student.class, id);
You can try after adding a default constructor to Student class along with your custom constructor.

Fetching data from table using hibernate

I am able to persist objects in relational database using hibernate.
please look at following code.
package one;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
#Entity
public class Customer {
#Id
private int customerId;
private String customerName;
private String customerAddress;
private int creditScore;
private int rewardPoints;
public Customer()
{
}
public Customer(int customerId,String customerName,String customerAddress,int creditScore,int rewardsPoints)
{
this.customerId=customerId;
this.customerAddress=customerAddress;
this.creditScore=creditScore;
this.customerName=customerName;
this.rewardPoints=rewardsPoints;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerAddress() {
return customerAddress;
}
public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}
public int getCreditScore() {
return creditScore;
}
public void setCreditScore(int creditScore) {
this.creditScore = creditScore;
}
public int getRewardPoints() {
return rewardPoints;
}
public void setRewardPoints(int rewardPoints) {
this.rewardPoints = rewardPoints;
}
}
Then to save object of this class i used following class. following class creates the object of class Customer and saves that object in database then again retrieves it and prints the CustomerName property of every saved object.
package one;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class TestCustomer {
public static void main(String args[])
{
Customer cust = new Customer(13,"Sara","Banglore",9000,60);
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
session.save(cust);
session.getTransaction().commit();
session.close();
session = factory.openSession();
session.beginTransaction();
List list = session.createQuery("FROM Customer").list();
Iterator iterator = list.iterator();
while(iterator.hasNext())
{
Customer custA = (Customer)iterator.next();
System.out.println("First Name\t"+custA.getCustomerName());
}
session.getTransaction().commit();
session.close();
}
}
I executed above code quite a number of times. code is running fine. it is able to fetch all objects which are saved.
but then i used oracle toad and fired a sql statement as
Insert into Customer(CUSTOMERID,CREDITSCORE,CUSTOMERNAME,REWARDPOINTS,CUSTOMERADDRESS)
VALUES(87,4000,'Saurabh',20,'Kalwa');
record gets stored in the table but when i execute above code, i am not able to fetch this record.
one conclusion i can draw is hibernate only returns persisted objects, but still is there any other way i can get all records ?
Are you sure you have submitted the record after inserting with toad for oracle?(you can open another client and execute a select to make sure it can be fetched from sql client).
If you want to debug, you can enable the sql logging function of hibernate, and then execute the sql which hibernate generates for your query in a sql client to make sure all the records can be fetched correctly.
And some suggestions for using JPA:
Make sure the #Entity has a name value which mapping to your physical table to avoid table mapping confusion.
Use #Column(name="column") for all your fields to mapping to the physical table column to avoid confusion.

java hibernate CLASS is not a bean

I have this simple bean name Brand.
I am able to create table out of him but i cant insert data (Mysql).
The error:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.hibernate.beans.Brand
This is the Bean:
package com.hibernate.beans;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity
public class Brand {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long brandId;
private String name;
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Long getBrandId() {
return brandId;
}
public void setBrandId(Long brandId) {
this.brandId = brandId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
this is how i try to insert data (and where i get the error):
Brand brand = new Brand();
brand.setName("test");
brand.setUrl("http://www.google.com");
Session ses = new AnnotationConfiguration().configure().buildSessionFactory().getCurrentSession();
Transaction t = ses.beginTransaction();
ses.save(brand);
t.commit();
this is how the table is created successfully:
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(Brand.class);
config.configure();
new SchemaExport(config).create(true, true);
The session factory you use to interact with the db needs to know about the entities.
In your case you need to change the code to add a Brand like this:
Brand brand = new Brand();
brand.setName("test");
brand.setUrl("http://www.google.com");
AnnotationConfiguration c = new AnnotationConfiguration();
c.addAnnotatedClass(Brand.class);
c.configure();
Session ses = c.buildSessionFactory().getCurrentSession();
Transaction t = ses.beginTransaction();
ses.save(brand);
t.commit();

Categories

Resources