java - Java中如何将一个POJO映射到另一个POJO?

标签 java mysql hibernate annotations

将 User 表映射到 UserProfilePersonalInfo 表以更新数据

在用户 POJO map 中

@OneToOne(mappedBy = "users",cascade = CascadeType.ALL)
private UserProfilePersonalInfo userProfilePersonalInfo;

在 UserProfilePersonalInfo POJO 中

@OneToOne
@JoinColumn(name = "user_id")
private User user;

在 hibernate 配置中

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new  
    LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] { "com.a2z.model" });
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
}
private Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect",  
    environment.getRequiredProperty("hibernate.dialect"));
    properties.put("hibernate.show_sql",
    environment.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.format_sql", 
    environment.getRequiredProperty("hibernate.format_sql"));
    return properties;        
}

我在控制台上遇到错误

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'usersService': Error creating bean with name 'usersService': Unsatisfied dependency expressed through field 'userdao': Error creating bean with name 'usersDao': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersDao': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersService': Unsatisfied dependency expressed through field 'userdao': Error creating bean with name 'usersDao': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersDao': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users
Sep 30, 2016 7:32:38 PM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'usersService': Error creating bean with name 'usersService': Unsatisfied dependency expressed through field 'userdao': Error creating bean with name 'usersDao': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersDao': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersService': Unsatisfied dependency expressed through field 'userdao': Error creating bean with name 'usersDao': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersDao': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/a2z/configuration/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.a2z.model.User.userProfilePersonalInfo, referenced property unknown: com.a2z.model.UserProfilePersonalInfo.users
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4718)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

最佳答案

mappedBy = "user"

这里是java字段名,不是sgbd字段名。提示:在 @annotation 中出现 Column 一词的地方使用 sgbd 字段名称

关于java - Java中如何将一个POJO映射到另一个POJO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39793778/

相关文章:

java - 值堆栈中的 Struts 2 数组列表

mysql - 优化分组依据和日期功能

php - 如何在数据库中搜索关键字并返回 50 英里范围内的数据

java - Hibernate 级联引用约束冲突

java - Hibernate:如何使用 HQL 选择空对象?

java - 登录后检索 CAS 票据

java - 禁用音量、返回和通话键

java - 是否可以使用 for 循环来创建单选按钮?

mysql:在一个查询中选择、插入、删除和更新

Spring第一次请求非常慢