使用基于java的配置时如何指定hibernate实体关联的映射类? (如@OneToOne等)

时间:2017-06-14 04:34:16

标签: java hibernate hibernate-mapping hibernate-5.x

我试图完全退出基于xml的映射,并使用基于java的配置编写相同的内容。在我需要使用实体映射之前,它工作得很好。有人可以帮助我如何通过基于java的配置指定hibernate映射吗?

以下是一个示例hibernate xml配置。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">


<hibernate-configuration>
<session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.username">myuser</property>
    <property name="hibernate.connection.password">mypassword</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/websystique</property>
    <property name="show_sql">true</property>
    <property name="format_sql">false</property>
    <mapping class="com.myapps.testapps.model.Student"/>
    <mapping class="com.myapps.testapps.model.University"/>
</session-factory>
</hibernate-configuration>

我想知道如何在HibernateConfig.java中指定以下映射

    <mapping class="com.myapps.testapps.model.Student"/>
    <mapping class="com.myapps.testapps.model.University"/>

基于Java的等效配置(HibernateConfig.java)。我正在跳过我的属性文件,这里提到了所有的hibernate属性。

@Configuration
@EnableTransactionManagement
@ComponentScan({"com.myapps.testapps.config"})
@PropertySource(value = ("classpath:application.properties"))


public class HibernateConfig {

@Autowired
private Environment environment;

@Bean
public LocalSessionFactoryBean sessionFactory(){
    System.out.println("-----------------------------sessionfactory--------------------------");
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[]{"com.myapps.testapps.model"});
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
}

@Bean
public DataSource dataSource(){
    System.out.println("-----------------------------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(){
    System.out.println("-----------------------------properties--------------------------");
    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){
    System.out.println("-----------------------------transaction manager--------------------------");
    HibernateTransactionManager txManager = new HibernateTransactionManager();
    txManager.setSessionFactory(s);
    return txManager;
}

}

我也使用了JPA注释如下

@Entity
@Table(name = "UNIVERSITY")
public class University {

@Id
@GeneratedValue
@Column(name = "UNIVERSITY_ID")
private long id;

@Column(name = "NAME")
private String name;

@Column(name = "COUNTRY")
private String country;

------------------------

@Entity
@Table(name = "STUDENT")
public class Student {

@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
private long id;

@Column(name = "FIRST_NAME")
private String firstName;

@Column(name = "LAST_NAME")
private String lastName;

@Column(name = "SECTION")
private String section;

@ManyToOne(optional = false)
@JoinColumn(name="UNIVERSITY_ID")
private University university;
----------------------

以及相关的getter和setter方法。

在一篇文章中我已经读过另一种方法是指定映射类并允许Hibernate查找映射文件:

Configuration cfg = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.addClass(org.hibernate.auction.Bid.class);

但我真的不知道如何将它添加到我的HibernateConfig.java文件中。如果我没有错,我认为必须使用sessionFactory完成。有人可以帮帮我吗?

因为在答案中提到setPackagesToScan()是等价的,当我使用已有setPackagesToScan()@Entity注释的现有代码的实体关联时,我收到以下错误。

Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.myapps.testapps.model.Student.university references an unknown entity: java.lang.String

1 个答案:

答案 0 :(得分:1)

XML配置中的<mapping>属性等同于setPackagesToScan()方法,该方法查找由@Entity注释注释的类,然后映射由其他JPA的注释完成。

我认为你的例子中没有任何错误...或者发布你得到的错误或什么不起作用。