title: How to specify hibernate entity association's mapping class when using java based configuration? (as in @OneToOne, etc.)
body: <p>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?</p>
  
  <p>Following is a sample hibernate xml configuration.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  
  &lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
      &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
      &lt;property name="hibernate.connection.username"&gt;myuser&lt;/property&gt;
      &lt;property name="hibernate.connection.password"&gt;mypassword&lt;/property&gt;
      &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/websystique&lt;/property&gt;
      &lt;property name="show_sql"&gt;true&lt;/property&gt;
      &lt;property name="format_sql"&gt;false&lt;/property&gt;
      &lt;mapping class="com.myapps.testapps.model.Student"/&gt;
      &lt;mapping class="com.myapps.testapps.model.University"/&gt;
  &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>I want to know how to specify the following mappings in my HibernateConfig.java</p>
  
  <pre><code>    &lt;mapping class="com.myapps.testapps.model.Student"/&gt;
      &lt;mapping class="com.myapps.testapps.model.University"/&gt;
  </code></pre>
  
  <p>Equivalent java based configuration (HibernateConfig.java). I am skipping my properties file which is where all the hibernate properties have been mentioned.</p>
  
  <pre><code>@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;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Also I have used JPA annotations as follows</p>
  
  <pre><code>@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;
  
  ------------------------
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@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;
  ----------------------
  </code></pre>
  
  <p>along with associated getter and setter methods.</p>
  
  <p>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:</p>
  
  <pre><code>Configuration cfg = new Configuration()
  .addClass(org.hibernate.auction.Item.class)
  .addClass(org.hibernate.auction.Bid.class);
  </code></pre>
  
  <p>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?</p>
  
  <hr>
  
  <p>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 <code>setPackagesToScan()</code> and <code>@Entity</code> annotations. </p>
  
  <p><code>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</code></p>