0

Hey I am brand new to hibernate and am trying to use hibernate with persistence to avoid using XML files.

This is my entity class

import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Lob;

@Entity
@Table(name = "TESTRUNS")
public class RunEntity {
@Id
@Column(name = "ID")
@GeneratedValue
private int ID;
@Column(name="TestNumber")
private int testNumber;
@Column(name="TestName")
private String testName;
@Column(name="Environment")
private String environment;
@Column(name="Source")
private String source;
@Column(name="PassOrFail")
private String passOrFail;
@Column(name="Date")
private Timestamp date;
@Column(name="ResultFiles")
private Lob resultFiles;
}

I guess my problem is that I dont know how to create a session that contains this table with the capability of adding and accessing RunEntry objects.

Thanks

user3073234
  • 771
  • 5
  • 11
  • 24
  • possible duplicate of [Hibernate hbm2ddl.auto possible values and what they do?](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do) – Zeus Jul 11 '14 at 13:59

2 Answers2

0

You can do it with the configuration. look for hbm2ddl config value.

<property name="hibernate.hbm2ddl.auto" value="validate">

reg: link

Zeus
  • 6,386
  • 6
  • 54
  • 89
0

In order to read or write any entities you need to have an entity manager. To get one, the code could look something like this:

private static final String PERSISTENCE_UNIT_NAME = "SOME_NAME";
private static final EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

// To persist an entity:
// create an instance of the entity
// set it up with the data you have
RunEntity re = new RunEntity();
re.setTestName("123");
re.setTestNumber(123);
.
.
.
// Get access to the entity manager
EntityManager em = ENTITY_MANAGER_FACTORY.createEntityManager();
em.getTransaction().begin();
em.persist(re);
em.getTransaction().commit();
em.close();

There is a lot more to JPA than this. You should look into JPA 2.0. You can use Hibernate's implementation but I would stay away from Hibernate specific functions as they will create a hard dependancy.