Hibernate 一对多数据获取 - MySQLSyntaxErrorException
Hibernate one-to-many data fetching - MySQLSyntaxErrorException
我正在尝试从数据库中获取数据 (MySQL)。我正在使用 Spring 4.2.4 和 Hibernate 5.0.7,以及 Java 8 和 Netbeans。到目前为止,它运行良好,但我的相关一对多关系并未与其余数据一起获取。我不断收到此信息:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'biuro.client_polisazycie' doesn't exist
它神奇地将2个table的名字连成了一个,这当然不存在...
我在检查 client.polisy 数据时出现错误,抛出异常:
@Override
public Client findByIdWithPolisa(int id) {
Client client = this.findById(id); //client does not have "polisy" data
Hibernate.initialize(client);
return client;
}
通常我会尝试在客户端对象中初始化 Set 数据,以避免急切获取(更喜欢惰性类型)。
这是客户实体:
@Entity
@Table(name = "client")
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
//...
@Basic(optional = false)
@Column(name = "creationDate")
@Temporal(TemporalType.DATE)
private Date creationDate;
@OneToMany(mappedBy = "", cascade = CascadeType.ALL)
private Set<Polisazycie> polisy;
//...skipping all getters and setters
让我说,如果我使用 mappedBy = "polisazycie" 它试图将数据提取到 "...(path).../Polisazycie.polisazycie" 所以它对我来说崩溃了。
这是 Polisazycie 实体:
@Entity
@Table(name = "polisazycie")
public class Polisazycie implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
//...
@JoinColumn(name = "idClient")
@ManyToOne
private Client client;
这是我的 HibernateConfiguration class:
@Configuration
@EnableTransactionManagement
@ComponentScan({"com.th.officesuiteservice.services", "com.th.officesuiteservice.dao"})
@PropertySource(value = {"classpath:application.properties"})
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[]{"com.th.officesuiteservice.model"});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
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;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
总结一下,当我获取客户端数据时,所有数据都被正确获取,只是 Set (Polisazycie) 和它试图从中获取的 table 名称是从 2 table秒。我在这里做错了什么?
您在 Polisazycie
映射的 mappedBy
属性中缺少一个值。试试这个
@OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
private Set<Polisazycie> polisy;
我正在尝试从数据库中获取数据 (MySQL)。我正在使用 Spring 4.2.4 和 Hibernate 5.0.7,以及 Java 8 和 Netbeans。到目前为止,它运行良好,但我的相关一对多关系并未与其余数据一起获取。我不断收到此信息:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'biuro.client_polisazycie' doesn't exist
它神奇地将2个table的名字连成了一个,这当然不存在... 我在检查 client.polisy 数据时出现错误,抛出异常:
@Override
public Client findByIdWithPolisa(int id) {
Client client = this.findById(id); //client does not have "polisy" data
Hibernate.initialize(client);
return client;
}
通常我会尝试在客户端对象中初始化 Set 数据,以避免急切获取(更喜欢惰性类型)。
这是客户实体:
@Entity
@Table(name = "client")
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
//...
@Basic(optional = false)
@Column(name = "creationDate")
@Temporal(TemporalType.DATE)
private Date creationDate;
@OneToMany(mappedBy = "", cascade = CascadeType.ALL)
private Set<Polisazycie> polisy;
//...skipping all getters and setters
让我说,如果我使用 mappedBy = "polisazycie" 它试图将数据提取到 "...(path).../Polisazycie.polisazycie" 所以它对我来说崩溃了。
这是 Polisazycie 实体:
@Entity
@Table(name = "polisazycie")
public class Polisazycie implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
//...
@JoinColumn(name = "idClient")
@ManyToOne
private Client client;
这是我的 HibernateConfiguration class:
@Configuration
@EnableTransactionManagement
@ComponentScan({"com.th.officesuiteservice.services", "com.th.officesuiteservice.dao"})
@PropertySource(value = {"classpath:application.properties"})
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[]{"com.th.officesuiteservice.model"});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
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;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
总结一下,当我获取客户端数据时,所有数据都被正确获取,只是 Set (Polisazycie) 和它试图从中获取的 table 名称是从 2 table秒。我在这里做错了什么?
您在 Polisazycie
映射的 mappedBy
属性中缺少一个值。试试这个
@OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
private Set<Polisazycie> polisy;