У меня проблема с получением записи из спящего режима недавно созданной записи.
Здесь создается фабрика сессий: -
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new
AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public Session openSession() {
//System.out.println("GOign to get session");
return sessionFactory.openSession();
}
}
вот класс модели: -
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="clients")
public class Clients implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
//file name started from capital letter bcoz of exist archtecture
private Integer id;
String clientname;
String org_id;
String org_type;
String sid;
String token;
Integer variable;
String oauthrefreshtoken;
String reg_date;
String email;
String reminderone;
String remindertwo;
String reminderthree;
String reminderfour;
Integer reminder ; //balance left after reminder to be sent
Float paypalamount;
String activationdate;
@OneToMany(cascade = { CascadeType.ALL},orphanRemoval=true)
@JoinColumn(name="client_id")
List<SmsNumbers> smsNumbers;
и их геттер и сеттер соответственно.
Ниже мой класс Дао
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import com.smsapp.model.Clients;
import com.smsapp.utility.HibernateUtil;
public class SmsDao {
public void addUpdateClients(Clients cli){
HibernateUtil objHibernateUtil = new HibernateUtil();
Session session=objHibernateUtil.openSession();
try{
session.beginTransaction();
Clients cliObj=prepareObjToSaveOrUpdate(cli);
session.saveOrUpdate(cliObj);
session.getTransaction().commit();
}catch(Exception ex)
{
ex.printStackTrace();
}
finally{
session.close();
}
}
public Clients prepareObjToSaveOrUpdate(Clients cli)
{
HibernateUtil objHibernateUtil = new HibernateUtil();
Session session = objHibernateUtil.openSession();
try{
Criteria criteria = session.createCriteria(Clients.class);
criteria.add(Restrictions.eq("org_id", cli.getOrg_id()));
Clients clients = (Clients) criteria.uniqueResult();
if (clients!=null) {
removeOrphans(clients.getId());
clients.getSmsNumbers().clear();
return prepareObjectToUpdate(clients,cli);
}
else
return prepareObjectTOSave(cli);
}finally {
session.close();
}
}
public Clients prepareObjectTOSave(Clients cli)
{
cli.setVariable(1);
cli.setEmail("");
cli.setReminderone("no");
cli.setRemindertwo("no");
cli.setReminderthree("no");
cli.setReminderfour("no");
cli.setReminder(1);
cli.setPaypalamount(new Float(0));
cli.setActivationdate("");
return cli;
}
public static void removeOrphans(Integer id)
{
System.out.println("going to remove data");
HibernateUtil objHibernateUtil = new HibernateUtil();
Session session = objHibernateUtil.openSession();
session.beginTransaction();
Query query = session.createQuery("delete SmsNumbers where
client_id = :ID");
query.setParameter("ID",id);
int result = query.executeUpdate();
session.getTransaction().commit();
session.close();
System.out.println("Data removed successfully");
}
public Clients getClientByPhoneNumber(String phone)
{
System.out.println("%%%%%%%%%%%%%%%%%\t"+phone);
Clients cli=null;
HibernateUtil objHibernateUtil = new HibernateUtil();
Session session = objHibernateUtil.openSession();
try{
Criteria criteria=session.createCriteria(Clients.class);
//criteria.createAlias("smsNumbers", "child");
//criteria.add(Restrictions.eq("child.number", phone));
List<Clients> lstCli=new ArrayList<Clients>();
lstCli=criteria.list();
System.out.println("%%%%%%%%%%%%%%%%%\t"+lstCli.size());
if(lstCli!=null && lstCli.size()>0 && lstCli.get(0)!=null){
//System.out.println("phone no matched in the database");
cli= lstCli.get(0);
System.out.println(cli.getId());
return cli;
}
}catch (Exception e) {
// TODO: handle exception
System.out.println(e);
return null;
}
finally{
session.close();
}
return cli;
}
}
После вставки клиента, когда я выбираю весь клиент, последняя созданная запись не получается.