2
我正在嘗試將spring的最新版本與最新版本的hibernate集成。一切都是基於註釋的。我得到的例外: -Hibernate 5註解org.hibernate.MappingException:未知實體
Caused by: org.hibernate.MappingException: Unknown entity: com.example.model.Customer
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1447) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:678) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
我在StackOverflow檢查了其他類似的問題,但沒有提供任何解決方案爲我工作。下面是代碼: -
@SpringBootApplication
@EnableTransactionManagement
public class SpringHibernateDemoApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringHibernateDemoApplication.class, args);
}
@Bean
public DataSource dataSource() throws ClassNotFoundException {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass((Class<? extends Driver>)Class.forName("com.mysql.jdbc.Driver"));
//BasicDataSource dataSource = new BasicDataSource();
//dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/customerDB");
dataSource.setUsername("root");
dataSource.setPassword("welcome");
return dataSource;
}
@Bean
public SessionFactory createSessionFactory() throws ClassNotFoundException {
Configuration configuration = new Configuration();
configuration.addAnnotatedClass(Customer.class);
Properties properties = new Properties();
properties.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.hbm2ddl.auto","update");
configuration.setProperties(properties);
StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
standardServiceRegistryBuilder.applySettings(configuration.getProperties());
standardServiceRegistryBuilder.applySetting(Environment.DATASOURCE,dataSource());
MetadataSources metadataSources = new MetadataSources(standardServiceRegistryBuilder.build());
//metadataSources.addPackage("");
return metadataSources.getMetadataBuilder().build().buildSessionFactory();
//return metadataSources.getMetadataBuilder().build().getSessionFactoryBuilder().build();
}
@Bean
public CustomerDao customerDao() {
return new CustomerDao();
}
@Bean
public HibernateTransactionManager hibernateTransactionManager() throws ClassNotFoundException {
return new HibernateTransactionManager(createSessionFactory());
}
@Override
public void run(String... args) throws Exception {
Customer customer = new Customer();
customer.setFistName("Amar");
customer.setLastName("Arya");
customer.setEmail("[email protected]");
customerDao().saveCustomer(customer);
}
}
模型對象是: -
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
//@GeneratorType()
private int id;
private String fistName;
private String lastName;
private String email;
public Customer(String fistName, String lastName, String email) {
this.fistName = fistName;
this.lastName = lastName;
this.email = email;
}
public Customer() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFistName() {
return fistName;
}
public void setFistName(String fistName) {
this.fistName = fistName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
DAO層是
public class CustomerDao extends AbstractDao<Customer> {
public void saveCustomer(Customer customer) {
save(customer);
}
}
@Transactional
public abstract class AbstractDao<E> {
@Autowired
protected SessionFactory sessionFactory;
public void save(E e) {
saveEntity(e);
}
private void saveEntity(E e) {
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.save(e);
transaction.commit();
}
}
這工作!!!!非常感謝。我還有另外一個問題。我如何添加多個文件? addPackage(「包名」)似乎不起作用。 –