Reputation: 31
I am trying to write some object into the database, I wrote the following code which is working fine but the problem is that because I am using a while loop, hibernate is treating every object as a new record, which causing the database to be filled with already existed value
My question is, which annotation or technique that I can use to only write the new records (new object's value) into the database, without writing the same object again incase it was written before (Insert only if not exists)
Here is my code
public class Parser {
public static void logParser() throws IOException {
// Creating the configuration object
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
// Ccreating the session factory object
SessionFactory factory = cfg.buildSessionFactory();
String fileName = "example.txt";
File logfile = new File("fileName");
Scanner scanner = new Scanner(new File(fileName), "UTF-8");
while (scanner.hasNext()) {
String s = scanner.nextLine();
if (s.startsWith("Start")) {
String[] logArray = s.split("\\|");
System.out.println(Arrays.toString(logArray));
// here is the session object
Session session = factory.openSession();
// transaction
Transaction t = session.beginTransaction();
// here is where the object is being written into the DB
NetGroup ng = new NetGroup();
ng.setNetGroup(logArray[2]);
session.persist(ng);
session.saveOrUpdate(ng);
t.commit();
session.close();
System.out.println("Successfully created an object and wrote its values into database ");
System.out.println();
}
}
scanner.close();
System.out.println("Successfully updated the Data Base");
}
}
Upvotes: 2
Views: 1888
Reputation: 1
I think you can use the primary key concept there to remove the duplicacy of rows and use the saveOrUpdate method.
If we work with hibernate API there should be one PK in the class.
session.saveOrUpdate(ng);
That above method check the record on the basis of primary key, if PK does not exists in the DB then it fire the insert query if exists then it update the same record rather than inserting new one.
public class NetGroup
{
private int primaryKeyId;
public int getprimaryKeyId() {
return primaryKeyId;
}
public void setprimaryKeyId(int primaryKeyId) {
this.primaryKeyId = primaryKeyId;
}
}
Upvotes: 0
Reputation: 2305
Your issue is happened because in each loop you create a new entity and persist it, then you commit and close the session, so when begin a new transaction in second loop there is no reference between the already persisted entity and second one, so new entity will be created.
You need to re find your entity then update it if existed, else create new one and persist it.
That code will fix your issue
public class Parser {
public static void logParser() throws IOException {
// Creating the configuration object
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
// Ccreating the session factory object
SessionFactory factory = cfg.buildSessionFactory();
String logFileName = "example.txt";
File logfile = new File("fileName");
Scanner scanner = new Scanner(new File(fileName), "UTF-8");
// here is the session object
Session session = factory.openSession();
// transaction
Transaction t = session.beginTransaction();
while (scanner.hasNext()) {
String s = scanner.nextLine();
if (s.startsWith("Start")) {
String[] logArray = s.split("\\|");
System.out.println(Arrays.toString(logArray));
Query query = session.createQuery("FROM " + getTableName() + " Where your condition "); // use unique filed
List<NetGroup> list = query.list();
if(list.size()==0){
NetGroup ng =new NetGroup();
}else{
ng =list.get(0);
}
ng.setNetGroup(logArray[2]);
session.saveOrUpdate(ng);
System.out.println("Successfully created an object and wrote its values into database ");
System.out.println();
}
}
session.close();
t.commit();
scanner.close();
System.out.println("Successfully updated the Data Base");
}
}
Make sure put valid condition instead of "your condition"
Upvotes: 2