- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:通过更新所有导入的库(hibernate、postgresql 等)解决了问题。
我想通过 PostgreSQL 中的本地数据库服务器设置一个 hibernate 小应用程序。我提前创建了包含所有引用和约束的数据库,因此我可以通过 JPA 从表生成实体。现在我删除了整个数据库(包括模式,无论如何都是“公共(public)”的),当我尝试进行测试并向数据库添加一些实体时,我收到一条错误消息“错误:错误:关系“angajat”不存在”
我已经检查了 SQL 方言是否正确,与数据库本地服务器的连接是否正确,我已经从预先创建的数据库生成了类,然后将其删除,所以我可以从我创建的实体中创建它,但这不起作用,因为这会给我带来更多错误。因此,我添加回了手动创建的数据库并尝试通过
“更新”它
错误减少了,但问题仍然存在。
AbstractEntity.java
package metamodel;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
@MappedSuperclass
public abstract class AbstractEntity implements Serializable {
private static final long serialVersionUID = -4803471783122679780L;
public static long getSerialVersionUID() {
return serialVersionUID;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
protected Long id;
@Version
@Column(name = "version")
private Integer version;
private String createdByUser;
private String updatedByUser;
private String entity_type = this.getClass().getSimpleName();
@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "dateCreated", nullable = false)
private Date dateCreated=Calendar.getInstance().getTime();
@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "dateUpdated", nullable = false)
private Date dateUpdated=Calendar.getInstance().getTime();
/**
* The default constructor is always needed for JPA.
*/
public AbstractEntity() {
super();
}
public AbstractEntity(Long id) {
this();
this.id = id;
}
@Override
public boolean equals(Object obj) {
if (id == null)
return super.equals(obj);
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final AbstractEntity other = (AbstractEntity) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public int hashCode() {
if (id == null)
return super.hashCode();
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
public String getCreatedByUser() {
return createdByUser;
}
public Date getDateCreated() {
return dateCreated;
}
public Date getDateUpdated() {
return dateUpdated;
}
public String getEntity_type() {
return entity_type;
}
public Long getId() {
return id;
}
public String getUpdatedByUser() {
return updatedByUser;
}
public Integer getVersion() {
return version;
}
public void setCreatedByUser(String createdByUser) {
this.createdByUser = createdByUser;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public void setDateUpdated(Date dateUpdated) {
this.dateUpdated = dateUpdated;
}
public void setEntity_type(String entity_type) {
this.entity_type = entity_type;
}
public void setId(Long id) {
this.id = id;
}
public void setUpdatedByUser(String updatedByUser) {
this.updatedByUser = updatedByUser;
}
public void setVersion(Integer version) {
this.version = version;
}
}
Angajat.java
package sgsm.model.entities;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import metamodel.AbstractEntity;
@Entity
@NamedQuery(name="Angajat.findAll", query="SELECT a FROM Angajat a")
public class Angajat extends AbstractEntity {
//@Id
@Column(unique = true)
private long codangajat;
private String email;
private String functie;
private String nume;
//bi-directional many-to-one association to Echipa
@OneToMany(mappedBy="angajat")
private List<Echipa> echipe;
// Contructors
public Angajat() {
}
public Angajat(long codangajat, String email, String functie, String nume, List<Echipa> echipe) {
super();
this.codangajat = codangajat;
this.email = email;
this.functie = functie;
this.nume = nume;
this.echipe = echipe;
}
// Getters && Setters
public long getCodangajat() {
return this.codangajat;
}
public void setCodangajat(long codangajat) {
this.codangajat = codangajat;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFunctie() {
return this.functie;
}
public void setFunctie(String functie) {
this.functie = functie;
}
public String getNume() {
return this.nume;
}
public void setNume(String nume) {
this.nume = nume;
}
public List<Echipa> getEchipe() {
return this.echipe;
}
public void setEchipe(List<Echipa> echipe) {
this.echipe = echipe;
}
public Echipa addEchipa(Echipa echipa) {
getEchipe().add(echipa);
echipa.setAngajat(this);
return echipa;
}
public Echipa removeEchipa(Echipa echipa) {
getEchipe().remove(echipa);
echipa.setAngajat(null);
return echipa;
}
}
TestAngajati.java
package sgsm.test.repository;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import junit.framework.Assert;
import sgsm.model.entities.Angajat;
import sgsm.model.repository.MasterRepository;
import sgsm.model.repository.MasterRepositoryDefault;
public class TestAngajati {
static MasterRepository repo = new MasterRepositoryDefault();
static String[] angajatiNume = {"Gherman", "Arama", "Teodoru", "Nedelcu"};
static String[] angajatiPrenume = {"Silviu", "Anca", "Rebeca", "Stefan"};
public static void main(String[] args) {
List<Angajat> x = repo.findAngajatAll();
if (x.size() == 0)
{
adaugaAngajati();
x = repo.findAngajatAll();
}
Assert.assertTrue( x.size() > 0);
}
public static void adaugaAngajati() {
Angajat g = null;
repo.beginTransaction();
for (int i=0; i<4; i++) {
g=new Angajat();
g.setCodangajat(1000+i);
g.setNume(angajatiNume[i]+" "+angajatiPrenume[i]);
g.setEmail(angajatiNume[i].toLowerCase()+angajatiPrenume[i].toLowerCase()+"@email.com");
g.setFunctie("Programator");
repo.addAngajat(g);
}
repo.commitTransaction();
}
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="SGSMPersistenceUnit"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.username" value="postgres" />
<property name="hibernate.connection.password" value="postgres" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="spring.jpa.hibernate.ddl-auto=create" value="true"/>
</properties>
</persistence-unit>
</persistence>
Expected: The test running successfully.
Actual: lots of errors
May 22, 2019 1:23:49 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
May 22, 2019 1:23:49 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.2.Final}
May 22, 2019 1:23:49 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
May 22, 2019 1:23:49 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
May 22, 2019 1:23:50 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
May 22, 2019 1:23:50 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
May 22, 2019 1:23:50 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
May 22, 2019 1:23:50 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/postgres/public]
May 22, 2019 1:23:50 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=postgres, password=****, autocommit=true, release_mode=auto}
May 22, 2019 1:23:50 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
May 22, 2019 1:23:50 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
May 22, 2019 1:23:50 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
May 22, 2019 1:23:50 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
May 22, 2019 1:23:51 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
May 22, 2019 1:23:51 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
May 22, 2019 1:23:51 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
May 22, 2019 1:23:51 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 42703
May 22, 2019 1:23:51 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: column t1.tgconstrname does not exist
Position: 113
May 22, 2019 1:23:51 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000299: Could not complete schema update
java.lang.NullPointerException
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.tool.hbm2ddl.DatabaseMetadata.getTableMetadata(DatabaseMetadata.java:129)
at org.hibernate.cfg.Configuration.generateSchemaUpdateScript(Configuration.java:1131)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:212)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:495)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1741)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:93)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at metamodel.AbstractRepository.<clinit>(AbstractRepository.java:30)
at sgsm.test.repository.TestAngajati.<clinit>(TestAngajati.java:21)
Hibernate:
select
angajat0_.id as id1_,
angajat0_.createdByUser as createdB2_1_,
angajat0_.dateCreated as dateCrea3_1_,
angajat0_.dateUpdated as dateUpda4_1_,
angajat0_.entity_type as entity5_1_,
angajat0_.updatedByUser as updatedB6_1_,
angajat0_.version as version1_,
angajat0_.codangajat as codangajat1_,
angajat0_.email as email1_,
angajat0_.functie as functie1_,
angajat0_.nume as nume1_
from
Angajat angajat0_
May 22, 2019 1:23:51 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 42703
May 22, 2019 1:23:51 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: column angajat0_.id does not exist
Position: 8
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: ERROR: column angajat0_.id does not exist
Position: 8
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1365)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1293)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:265)
at sgsm.model.repository.MasterRepositoryDefault.findAngajatAll(MasterRepositoryDefault.java:98)
at sgsm.test.repository.TestAngajati.main(TestAngajati.java:27)
Caused by: org.hibernate.exception.SQLGrammarException: ERROR: column angajat0_.id does not exist
Position: 8
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at com.sun.proxy.$Proxy18.executeQuery(Unknown Source)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1962)
at org.hibernate.loader.Loader.doQuery(Loader.java:829)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
at org.hibernate.loader.Loader.doList(Loader.java:2447)
at org.hibernate.loader.Loader.doList(Loader.java:2433)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2263)
at org.hibernate.loader.Loader.list(Loader.java:2258)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1215)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:256)
... 2 more
Caused by: org.postgresql.util.PSQLException: ERROR: column angajat0_.id does not exist
Position: 8
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2096)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1829)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:510)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:386)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:271)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 17 more
最佳答案
Hibernate 错误提示 Angajat.java 中不存在列 id
,由于您对 codangajat 列有 @Column(unique = true)
规范,因此不妨使用 @Id
注释。
您应该使用 PostgreSQL82Dialect
,因为 PostgreSQLDialect
已弃用。来源 - org.hibernate.dialect.PostgreSQLDialect is deprecated
您知道 PostGreSQL 方言区分大小写 - 对吧?
您的@NamedQuery
由大写A表名和Angajat组成,而您的@OneToMany
由小写组成a angajat。而且您没有提到您的数据库表名称实际上是什么情况。
更新您的代码并使其区分大小写,然后就可以开始了。
关于java - SQLGrammarException : ERROR: relation "angajat" does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56254803/
reqwest v0.9 将 serde v1.0 作为依赖项,因此实现 converting serde_json errors into reqwest error . 在我的代码中,我使用 se
我有这个代码: let file = FileStorage { // ... }; file.write("Test", bytes.as_ref()) .map_err(|e| Mu
我只是尝试用angular-cli创建一个新项目,然后运行服务器,但是它停止并显示一条有趣的消息:Error: No errors。 我以这种方式更新了(希望有帮助):npm uninstall -g
我从我的 javascript 发送交易 Metamask 打开传输对话框 我确定 i get an error message in metamask (inpage.js:1 MetaMask -
这个问题在这里已经有了答案: How do you define custom `Error` types in Rust? (3 个答案) How to get a reference to a
我想知道两者之间有什么大的区别 if let error = error{} vs if error != nil?或者只是人们的不同之处,比如他们如何用代码表达自己? 例如,如果我使用这段代码: u
当我尝试发送超过 50KB 的图像时,我在 Blazor 服务器应用程序上收到以下错误消息 Error: Connection disconnected with error 'Error: Serv
我有一个error-page指令,它将所有异常重定向到错误显示页面 我的web.xml: [...] java.lang.Exception /vi
我有这样的对象: address: { "phone" : 888, "value" : 12 } 在 WHERE 中我需要通过 address.value 查找对象,但是在 SQL 中有函数
每次我尝试编译我的代码时,我都会遇到大量错误。这不是我的代码的问题,因为它在另一台计算机上工作得很好。我尝试重新安装和修复,但这没有帮助。这是整个错误消息: 1>------ Build starte
在我的代码的类部分,如果我写一个错误,则在不应该的情况下,将有几行报告为错误。我将'| error'放在可以从错误中恢复的良好/安全位置,但是我认为它没有使用它。也许它试图在某个地方恢复中间表情? 有
我遇到了 csv 输入文件整体读取故障的问题,我可以通过在 read_csv 函数中添加 "error_bad_lines=False" 来删除这些问题来解决这个问题。 但是我需要报告这些造成问题的文
在 Spring 中,验证后我们在 controller 中得到一个 BindingResult 对象。 很简单,如果我收到验证错误,我想重新显示我的表单,并在每个受影响的字段上方显示错误消息。 因此
我不知道出了什么问题,因为我用 Java 编程了大约一年,从来没有遇到过这个错误。在一分钟前在 Eclipse 中编译和运行工作,现在我得到这个错误: #A fatal error has been
SELECT to_char(messages. TIME, 'YYYY/MM/DD') AS FullDate, to_char(messages. TIME, 'MM/DD
我收到这些错误: AnonymousPath\Anonymized.vb : error BC30037: Character is not valid. AnonymousPath\Anonymiz
我刚刚安装了 gridengine 并在执行 qstat 时出现错误: error: commlib error: got select error (Connection refused) erro
嗨,我正在学习 PHP,我从 CRUD 系统开始,我在 Windows 上安装了 WAMP 服务器,当我运行它时,我收到以下错误消息。 SCREAM: Error suppression ignore
我刚刚开始一个新项目,我正在学习核心数据教程,可以找到:https://www.youtube.com/watch?v=zZJpsszfTHM 我似乎无法弄清楚为什么会抛出此错误。我有一个名为“Exp
当我使用 Jenkins 运行新构建时,出现以下错误: "FilePathY\XXX.cpp : fatal error C1853: 'FilePathZ\XXX.pch' precompiled
我是一名优秀的程序员,十分优秀!