我正在尝试使用JPA + Hibernate设置我的Oracle数据库。我将Intellij连接到我的数据源,然后使用数据库模式生成持久性映射。但是,每次运行以下代码时,我都会得到一个
错误(java.sql.SQLException:ORA-00942:表或视图不存在):
public static void main(String[] args) {
System.out.println("Maven + Hibernate + Oracle");
PersistenceProvider persistenceProvider = new HibernatePersistence();
EntityManagerFactory entityManagerFactory = persistenceProvider.createEntityManagerFactory(
"NewPersistenceUnit",new HashMap());
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
AdministrationUser user = new AdministrationUser();
user.setUserName("mjh");
user.setName("Brendan");
user.setCreateDate(Timestamp.valueOf("2007-09-23 10:10:10.0"));
user.setCreateBy("Brendan");
user.setModDate(Timestamp.valueOf("2007-09-23 10:10:10.0"));
user.setModBy("Brendan");
entityManager.persist(user);
entityManager.getTransaction().commit();
}
persistence.xml和persistence类如下所示:
public class AdministrationUser {
private long administrationUserId;
private String userName;
private String name;
private Timestamp createDate;
private String createBy;
private Timestamp modDate;
private String modBy;
@Id
@Column(name = "administrationUserId", nullable = false, insertable = true, updatable = true, precision = 0)
public long getAdministrationUserId() {
return administrationUserId;
}
public void setAdministrationUserId(long administrationUserId) {
this.administrationUserId = administrationUserId;
}
@Basic
@Column(name = "userName", nullable = true, insertable = true, updatable = true, length = 64)
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Basic
@Column(name = "name", nullable = true, insertable = true, updatable = true, length = 64)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "createDate", nullable = true, insertable = true, updatable = true)
public Timestamp getCreateDate() {
return createDate;
}
public void setCreateDate(Timestamp createDate) {
this.createDate = createDate;
}
@Basic
@Column(name = "createBy", nullable = true, insertable = true, updatable = true, length = 15)
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
@Basic
@Column(name = "modDate", nullable = true, insertable = true, updatable = true)
public Timestamp getModDate() {
return modDate;
}
public void setModDate(Timestamp modDate) {
this.modDate = modDate;
}
@Basic
@Column(name = "modBy", nullable = true, insertable = true, updatable = true, length = 15)
public String getModBy() {
return modBy;
}
public void setModBy(String modBy) {
this.modBy = modBy;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="NewPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>edu.rit.its.coopEval.model.AdministrationUser</class>
<properties>
<property name="hibernate.connection.url" value=""/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
我故意删除了网址和密码以及用户名,但在我的代码中它们是正确的,因为我使用了完全相同的网址,用户名和密码来连接到toad中的数据库并创建表格。我不知道问题是什么。
答案 0 :(得分:0)
您可以尝试添加:
<property name="javax.persistence.schema-generation.database.action" value="create"/>
到你的镇定单位。否则,我猜你需要为你的实体添加@Table(name = "<whatever its called>")
。
编辑:
经过我的一些研究,看起来对于Hibernate来说,你可能需要这样做:
<property name="hibernate.hbm2ddl.auto">update</property>
我不知道是否将它设置为属性,就像你正在做的那样。