简体   繁体   中英

Hibernate one-to-many data fetching - MySQLSyntaxErrorException

I am trying to fetch my data from database (MySQL). I am using Spring 4.2.4 and Hibernate 5.0.7, and Java 8 with 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:

    @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).

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.

Here is Polisazycie entity:

@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:

@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. What am I doing wrong here?

You are missing a value in mappedBy attribute on Polisazycie mapping. Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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