У меня есть этот простой бренд bean.
Я могу создать таблицу из него, но я не могу вставить данные (Mysql).
Ошибка:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.hibernate.beans.Brand
Это 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;
}
}
вот как я пытаюсь вставить данные (и где я получаю ошибку):
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();
так успешно создается таблица:
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(Brand.class);
config.configure();
new SchemaExport(config).create(true, true);
Сессия factory, которую вы используете для взаимодействия с db, должна знать о сущности.
В вашем случае вам нужно изменить код, чтобы добавить бренд следующим образом:
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();
javaassist.jar
отсутствует).