title: How to build a has-a-relationship connection between a Pojo class and an abstract class using Hibernate Annotations
body: <p>I have a class named Driver.Java, another class named Truck.Java and an abstract class named Vehicle.Java. A driver can drive several vehicles, truck, bus etc. I want to simulate this situation. Driver.Class and Vehicle.Class connects each other with has-a-relationship. Codes are below.</p>
  
  <p>Driver.Java</p>
  
  <pre><code>@Entity
  @Table(name="DRIVER")
  
  public class Driver {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name="ID")
  private int driverId;
  
  @OneToOne(cascade=CascadeType.ALL)
  private Vehicle vehicle;
  
  @Column(name="NAME")
  private String name;
  
  @Column(name="AGE")
  private int age;
  
  public Vehicle getVehicle() {
      return vehicle;
  }
  public void setVehicle(Vehicle vehicle) {
      this.vehicle = vehicle;
  }
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public int getAge() {
      return age;
  }
  public void setAge(int age) {
      this.age = age;
  }
  public Driver(String name, int age) {
      this.name = name;
      this.age = age;
  }
  public void setDriverId(int driverId) {
      this.driverId = driverId;
  }
  public int getDriverId() {
      return driverId;
  }
  </code></pre>
  
  <p>Vehicle.Java</p>
  
  <pre><code>@MappedSuperclass
  public abstract class Vehicle {
  
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name="ID")
  private int id;
  
  @Column(name="BRAND")
  private String brand;
  
  @Column(name="COLOUR")
  private String colour;
  
  @Column(name="PRICE")
  private int price;
  
  public String getBrand() {
      return brand;
  }
  public void setBrand(String brand) {
      this.brand = brand;
  }
  public String getColour() {
      return colour;
  }
  public void setColour(String colour) {
      this.colour = colour;
  }
  public int getPrice() {
      return price;
  }
  public void setPrice(int amount) {
      this.price = amount;
  }
  
  public abstract int totalCost();
  public Vehicle(String brand, String colour, int price) {
      this.brand = brand;
      this.colour = colour;
      this.price = price;
  }
  public void setId(int id) {
      this.id = id;
  }
  public int getId() {
      return id;
  }
  
  public Vehicle(){}  
  }
  </code></pre>
  
  <p>Main</p>
  
  <pre><code>    Vehicle vehicle2 = new Truck("Mercedes", "White", 250);
      Driver driver = new Driver("Hakan Namlı", 36);
      driver.setVehicle(vehicle2);
      DriverDAO dDao = new DriverDAO();
      dDao.addDriver(driver); 
  </code></pre>
  
  <p>DriverDAO.Java</p>
  
  <pre><code>public class DriverDAO {
  
  private Session session;
  
  public void addDriver(Driver driver){
  
      try {
          session = HibernateUtil.getSessionFactory().openSession();
          session.getTransaction().begin();
          session.save(driver);
          session.getTransaction().commit();
      }
      catch(HibernateException ex){
          session.getTransaction().rollback();
          ex.printStackTrace();
      }
      finally{
          session.close();
      }
  }
  }
  </code></pre>
  
  <p>HibernateUTIL.Java
      public class HibernateUtil {</p>
  
  <pre><code>private static final SessionFactory sessionFactory;
  
  static {
      try {
          sessionFactory = new AnnotationConfiguration()
          .configure()
          .addPackage("models") 
          .addAnnotatedClass(Driver.class)
          .addAnnotatedClass(Vehicle.class)
          .addAnnotatedClass(Truck.class)
          .addAnnotatedClass(Bus.class)
          .buildSessionFactory();
  
      } catch (Throwable ex) {
          throw new ExceptionInInitializerError(ex);
      }
  }
  
  public static SessionFactory getSessionFactory() {
      return sessionFactory;
  }
  }
  </code></pre>
  
  <p>hibernate.cfg.xml</p>
  
  <pre><code>&lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property      
       name="hibernate.connection.driver_class"&gt;oracle.jdbc.driver.OracleDriver&lt;/property&gt;
      &lt;property name="hibernate.connection.url"&gt;...&lt;/property&gt;
      &lt;property name="hibernate.connection.username"&gt;...&lt;/property&gt;
      &lt;property name="hibernate.connection.password"&gt;...&lt;/property&gt;
      &lt;property name="hibernate.connection.pool_size"&gt;10&lt;/property&gt;
      &lt;property name="show_sql"&gt;true&lt;/property&gt;
      &lt;property name="dialect"&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt;
      &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt;   
  &lt;/session-factory&gt;
  </code></pre>
  
  <p></p>
  
  <p>This is the output:</p>
  
  <pre><code>1 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.5.2-Final
  11 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.5.2-Final
  12 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
  14 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
  17 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
  75 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
  79 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
  79 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
  128 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
  128 [main] INFO org.hibernate.cfg.AnnotationConfiguration - Mapping package models
  146 [main] WARN org.hibernate.cfg.AnnotationBinder - Package not found or wo package-info.java: models
  154 [main] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
  177 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: models.Driver
  201 [main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity models.Driver on table DRIVER
  238 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: models.Truck
  239 [main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity models.Truck on table TRUCK
  241 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: models.Bus
  241 [main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity models.Bus on table BUS
  Exception in thread "main" java.lang.NullPointerException
      at dao.DriverDAO.addDriver(DriverDAO.java:28)
      at main.Main.main(Main.java:39)
  </code></pre>
  
  <p>When i try to run this, object of SessionFactory class is returned null. So, i cannot generate a Session object and cannot save the data. It is not about hibernate config file, because i tried it in different scenarios. Any help will be appreciated. Thank you.</p>