Hibernate一对多数据获取--MySQLSyntaxErrorException

时间:2016-02-17 10:41:48

标签: java mysql hibernate one-to-many

我正在尝试从数据库(MySQL)中获取数据。我使用的是Spring 4.2.4和Hibernate 5.0.7,以及使用Netbeans的Java 8。到目前为止,它工作得很好,但我的相关的一对多关系并没有与其他数据相关联。我一直收到这个信息:

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

它神奇地将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;
}

通常我试图初始化客户端对象内部的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”,它会尝试将数据提取到“...(路径)... / 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类:

@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)和它试图获取的表名从2个表组合而来。我在这做错了什么?

1 个答案:

答案 0 :(得分:1)

您在mappedBy映射的Polisazycie属性中缺少值。试试这个

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