嘿,我是hibernate的新手,我正在尝试使用hibernate和持久性来避免使用XML文件。
这是我的实体类
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;
}
我想我的问题是我不知道如何创建一个包含该表的会话,并且能够添加和访问RunEntry对象。
由于
答案 0 :(得分:0)
答案 1 :(得分:0)
为了读取或写入任何实体,您需要拥有实体管理器。为了获得一个,代码看起来像这样:
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();
JPA还有很多东西。你应该看看JPA 2.0。你可以使用Hibernate的实现,但是我会远离Hibernate特定的功能,因为它们会产生很大的依赖性。