1

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

eccentricCoder
  • 846
  • 1
  • 14
  • 35
  • 1
    You need a way to map your entities to the relational model ? If such is the case use jpa annotations. – Sahil Gupta Jun 14 '17 at 04:41
  • @SahilGupta I am already using jpa annotations. I have edited my question to show the same – eccentricCoder Jun 14 '17 at 05:10
  • 1
    Are you using spring framework ? – Sahil Gupta Jun 14 '17 at 05:12
  • Yes sir, I am using Spring MVC 4 and I havnt mentioned it in tags since this is hibernate related question. I am not sure if I am allowed to mention that in tags. – eccentricCoder Jun 14 '17 at 05:13
  • sessionFactory.setPackagesToScan(new String[]{"com.myapps.testapps.model"}); Ideally this should work. Did you mentioned the package name correctly ? You could also use @EntityScan annotation – Sahil Gupta Jun 14 '17 at 05:18
  • @SahilGupta am using JPA annotations if that is wht you meant and package names are all mentioned correctly. and I am already using setPackagesToScan() as you can find in the code posted along with my question. – eccentricCoder Jun 14 '17 at 05:22
  • 1
    So can you please clarify then what are the hibernate mappings that you are not able to do with java conf. – Sahil Gupta Jun 14 '17 at 05:24
  • @SahilGupa Thankyou for mentioning, I forgot to mention that in my question. I have edited my question to show the same. – eccentricCoder Jun 14 '17 at 05:28
  • Why do you need those hibernate mappings ? You already mentioned packages to scan. – Sahil Gupta Jun 14 '17 at 05:49
  • @SahilGupta Because it returns following error --> `@OneToOne` or `@ManyToOne` on ........... references an unknown entity: java.lang.String. Can you please shared how you used java based configuration in your code? – eccentricCoder Jun 14 '17 at 05:59
  • 1
    While asking a question please do mention the errors you are getting as well.. it helps – Sahil Gupta Jun 14 '17 at 06:00
  • @SahilGupta I assumed this was the wrong way to do it. That is why I didnt post the error along. I have now updated my question with the error. Thankyou for mentioning – eccentricCoder Jun 14 '17 at 06:16
  • In the @manytoone set the attribute target to the university class and try . Ideally that should not be a problem but try once . – Sahil Gupta Jun 14 '17 at 10:51

1 Answers1

1

The <mapping> attributes in your XML configuration is equivalent to the setPackagesToScan() method that looks for classes annotated by @Entity annotation and then the mapping is done by the others JPA's annotations.

I don't think there's anything wrong in your example... Or post the error you get or what doesn't work.

Matthieu Saleta
  • 1,388
  • 1
  • 11
  • 17
  • I assumed this was the wrong way to do it. That is why I didnt post the error along. I have now updated my question with the error. – eccentricCoder Jun 14 '17 at 06:16