简体   繁体   English

休眠一对多数据获取-MySQLSyntaxErrorException

[英]Hibernate one-to-many data fetching - MySQLSyntaxErrorException

I am trying to fetch my data from database (MySQL). 我正在尝试从数据库(MySQL)中获取数据。 I am using Spring 4.2.4 and Hibernate 5.0.7, and Java 8 with Netbeans. 我正在使用Spring 4.2.4和Hibernate 5.0.7,以及Java 8和Netbeans。 So far it was working great, but my related one-to-many relationship is not fetched with the rest of data. 到目前为止,它运行良好,但是我与相关的一对多关系并未与其余数据一起获取。 I keep getting this info: 我不断收到此信息:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'biuro.client_polisazycie' doesn't exist

It magically connects name of 2 tables into one, which of course does not exists... Error shows up here while I am checking client.polisy data, which throws that exception: 它神奇地将2个表的名称连接到一个表中,这当然是不存在的。当我检查client.polisy数据时,这里显示错误,并抛出该异常:

    @Override
public Client findByIdWithPolisa(int id) {
    Client client = this.findById(id); //client does not have "polisy" data
    Hibernate.initialize(client); 
    return client;
}

Generally I tried to initialize Set data inside of client object in order to avoid fetching eager (prefer lazy type). 通常,我尝试在客户端对象内部初始化Set数据,以避免获取渴望的数据(首选惰性类型)。

Here is Client entity: 这是客户实体:

@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

Let me say that if I use mappedBy = "polisazycie" it is trying to fetch data into "...(path).../Polisazycie.polisazycie" so it crashes for me then. 让我说,如果我使用mapledBy =“ polisazycie”,它正在尝试将数据获取到“ ...(path)... / Polisazycie.polisazycie”中,因此它崩溃了。

Here is Polisazycie entity: 这是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;

And here is my HibernateConfiguration class: 这是我的HibernateConfiguration类:

@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;
}

} }

So to summarize, while I fetch Client data, all of them are fetched properly, just not the Set (Polisazycie) and the table name which it is trying to fetch from is combined from 2 tables. 综上所述,当我获取客户端数据时,所有数据都被正确地获取了,只是没有集合(Polisazycie),并且它试图从中获取的表名是从2个表中组合而成的。 What am I doing wrong here? 我在这里做错了什么?

You are missing a value in mappedBy attribute on Polisazycie mapping. 你缺少一个值mappedBy的属性Polisazycie映射。 Try this 尝试这个

@OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
private Set<Polisazycie> polisy;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM