gpt4 book ai didi

java - org.hibernate.MappingException : Unknown entity: library. 模型.Person

转载 作者:行者123 更新时间:2023-12-01 22:54:57 25 4
gpt4 key购买 nike

我一整天都在努力解决这个问题,但还没有找到解决方案。

我有一个到数据库表的简单实体映射:

@Entity
@Table(name = "PERSON")
public class Person implements Serializable {

// Attributes.
@Id
@Column(name = "ID", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

@Column(name = "NAME", nullable = false, length=50)
private String name;

@Column(name = "ADDRESS", nullable = false, length=100)
private String address;

@Column(name = "TELEPHONE", nullable = false, length=10)
private String telephone;

@Column(name = "EMAIL", nullable = false, length=50)
private String email;

@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="PERSON_ID")
private List<Book> books;

// Constructors.
public Person() {
this.id = 0;
this.name = ("");
this.address = ("");
this.telephone = ("");
this.email = ("");
this.books = null;
}

该实体由 Spring Controller 通过以下方法控制:

@RequestMapping(value = "/register", method = RequestMethod.POST)
public String post(@ModelAttribute("person") Person person,
BindingResult bindingResult,
SessionStatus sessionStatus,
Model model /* ,
HttpSession session */) throws DAOException {
logger.info(PersonController.class.getName() + ".post() method called.");

personValidator.validate(person, bindingResult);
if (bindingResult.hasErrors()) {
return "register";
}
else {

// Write Person to database.
personService.insert(person);

// Set view.
sessionStatus.setComplete();
return "options";
}
}

服务类别是:

@Service("personService")
@Transactional
public class PersonService {

@Autowired
PersonDAO personDAO;

public void insert(Person person) throws DAOException {
personDAO.insert(person);
}

DAO如下:

@Repository("PersonDAO")
public class PersonDAOImpl implements PersonDAO {

@Autowired
private SessionFactory sessionFactory;
static final Logger logger = Logger.getLogger(PersonDAOImpl.class.getName());

@Override
public void insert(Person person) throws DAOException {
logger.info(PersonDAOImpl.class.getName() + ".insert() method called.");

Session session = sessionFactory.openSession();
Transaction transaction = session.getTransaction();
try {
transaction.begin();
session.save(person);
transaction.commit();
}
catch(RuntimeException e) {
transaction.rollback();
throw new DAOException(e);
}
finally {
session.close();
}
}

当然还有我的 Spring servlet 将所有内容绑定(bind)在一起的部分:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class ">thread</prop>

<!-- What to do with the database schema. -->
<prop key="hbm2ddl.auto">validate</prop>
<!-- validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema at the end of the session. -->
</props>
</property>
</bean>

那么为什么我会收到消息:

org.hibernate.MappingException: Unknown entity: library.model.Person

如果我尝试这样的事情:

<property name="packagesToScan" value="library.model" /> 

<property name="annotatedClasses">
<list>
<value>library.model.Person</value>
</list>
</property>

目前我得到:

Jun 15, 2014 10:50:58 PM org.apache.catalina.core.ApplicationContext log INFO: Initializing Spring root WebApplicationContext Jun 15, 2014 10:51:02 PM org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: library.services.PersonService library.controller.PersonController.personService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: library.dao.PersonDAOImpl library.services.PersonService.personDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PersonDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory library.dao.PersonDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/library-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/persistence/NamedStoredProcedureQuery at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:744)

最佳答案

尝试替换:

<property name="annotatedClasses">
<list>
<value>library.model.Person</value>
</list>
</property>

与:

<property name="packagesToScan">
<list>
<value>library.model</value>
</list>
</property>

在您的 sessionFactory 属性中。

如果您收到错误消息,指出无法找到属性“packagesToScan”,那么您应该将 spring-orm 库添加到您的项目中。如果您使用maven,那么您可以添加:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>YOUR SPRING VERSION HERE</version>
</dependency>

关于java - org.hibernate.MappingException : Unknown entity: library. 模型.Person,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24233904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com