I am trying to completely opt out of xml based mapping and write the same using java based configuration. It was working completely fine until I had the requirement of using entity mappings. Can someone help me with how to specify hibernate mapping through java based configuration?
Following is a sample hibernate xml configuration.
<?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>
I want to know how to specify the following mappings in my HibernateConfig.java
<mapping class="com.myapps.testapps.model.Student"/>
<mapping class="com.myapps.testapps.model.University"/>
Equivalent java based configuration (HibernateConfig.java). I am skipping my properties file which is where all the hibernate properties have been mentioned.
@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;
}
}
Also I have used JPA annotations as follows
@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;
------------------------
and
@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;
----------------------
along with associated getter and setter methods.
In an article I have read that an alternative way is to specify the mapped class and allow Hibernate to find the mapping document is:
Configuration cfg = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.addClass(org.hibernate.auction.Bid.class);
But I really dont have a clue on how to add this to my HibernateConfig.java file. If I am not wrong I assume that this have to be done using the sessionFactory. Can somebody please help me with this?
Since in the answers it is mentioned that setPackagesToScan() is the equivalent for , I am getting the following error when I used the Entity associations with my existing code which already has setPackagesToScan()
and @Entity
annotations.
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