<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/PP</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
</session-factory>
</hibernate-configuration>
HibernateSession.javaComment configurer correctement Hibernate
public class HibernateSession {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
DataFetcher.java utilisé pour exécuter quesries
public class DataFetcher {
Session session;
public DataFetcher()
{
session = HibernateSession.getSessionFactory().getCurrentSession();
}
public void Save(Marki m)
{
org.hibernate.Transaction tx = session.beginTransaction();
session.save(m);
session.getTransaction().commit();
}
}
ManagedBean(name="dodaj")
@ViewScoped
public class DodajOferte implements Serializable {
private Marki marka;
public DodajOferte()
{
marka = new Marki();
}
public String Dodaj()
{
DataFetcher f = new DataFetcher();
try {
f.Save(marka);
} catch (HibernateException ex) {
System.out.println(ex.getMessage().toString());
FacesMessage message = new FacesMessage("err");
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, message);
return null;
}
return null;
}
public Marki getMarka()
{
return marka;
}
public void setMarka(Marki marka)
{
this.marka = marka;
}
}
Comment configurer correctement je ne Hibernate? J'ai des problèmes avec almogs chaque transaction que je fais! Dans ce cas, je reçois "INFO: tentative illégale d'associer une collection à deux sessions ouvertes". Quand je change session.save(m); to session.merge(m);
cela fonctionne aussi longtemps que je suis sur la même page. Lorsque je change de page et y retourne, j'obtiens beaucoup d'exceptions.
ressemble à la fusion isnt fonctionne plus. – user1997553