Olá, consegui fazer ele ao menos reconhecer q estou a tentar inserir a entidade cliente… como estou a usar annotations entao eu pude configurar diretamente na minha classe que cria a sessao.
public class SessionManager {
private static SessionFactory sessionFactory;
private static Configuration configuration;
static {
try {
configuration = new Configuration();
sessionFactory = configuration
.setProperty("hibernate.connection.username", "root")
.setProperty("hibernate.connection.password", "123456")
.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/TESTEDB")
.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect")
.setProperty("hibernate.hbm2ddl.auto", "update")
.setProperty("show_sql", "true")
.setProperty("format_sql", "true")
.addAnnotatedClass(BasicEntity.class)
.addAnnotatedClass(Client.class)
.buildSessionFactory();
} catch (Throwable ex) {
// Log exception!
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return sessionFactory.openSession();
}
public static Configuration getConfiguration() {
if( configuration == null )
configuration = new Configuration();
return configuration;
}
}
Porém ainda encontro problemas. . quando tento persistir ele nao joga excessao nenhuma, mas tambem nao insere no banco… eis as minhas classes dao e entidades:
public class CrudDao <ENTITY extends BasicEntity>{
public void persist(ENTITY entity) throws SQLException{
Session session = SessionManager.getSession();
session.persist(entity);
session.flush();
SessionManager.getConfiguration().buildSettings().getConnectionProvider().
getConnection().commit();
}
public void delete(ENTITY entity) throws SQLException{
Session session = SessionManager.getSession();
session.delete(entity);
session.flush();
SessionManager.getConfiguration().buildSettings().getConnectionProvider().
getConnection().commit();
}
}
public class ClientDao extends CrudDao<Client>{
}
Agora minha entidade . que estende a basicEntity
@MappedSuperclass
public abstract class BasicEntity {
public abstract Long getId();
public abstract void setId(Long id);
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BasicEntity other = (BasicEntity) obj;
if (getId() == null) {
if (other.getId() != null)
return false;
} else if (!getId().equals(other.getId()))
return false;
return true;
}
}
@Entity(name = "cliente")
public class Client extends BasicEntity{
@Id
@Column(name = "ID")
private Long id;
@Column(name="NOME")
private String name;
@Column(name="CNPJ")
private String cnpj;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCnpj() {
return cnpj;
}
public void setCnpj(String cnpj) {
this.cnpj = cnpj;
}
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
}
obs: id é auto increment mas da erro se eu tento inserir sem um id… nao sei se essa informacao é util ou nao…
a unica coisa que retorna no console quando tento salvar um cliente é …
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Alguem sabe o q pode ser isso?
desde ja . agradeco.