我读过一些类似的问题,但我的问题是“有点不同”。
我的实体就像
@Entity
@Table(name = "commission_template")
public class CommmissionTemplate {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private Double min;
private Double max;
private Double value;
private boolean active;
@Convert(converter = LocalDateToSqlConverter.class)
private LocalDate start;
@Convert(converter = LocalDateToSqlConverter.class)
private LocalDate end;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "commission_template_commission_type",
joinColumns = {@JoinColumn(name = "commission_template_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "type_id", referencedColumnName = "name")}
)
private CommissionType type;
// getters/setters etc.
}
和persistence.xml
中的配置类似
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="${db.dialect}"/>
然后,当我在日志中启动y应用程序时,我已经:
Hibernate: create table commission_template (id int8 not null, active boolean not null, end timestamp, max float8, min float8, start timestamp, value float8, primary key (id))
Hibernate: create table commission_template_commission_type (type_id varchar(255), commission_template_id int8 not null, primary key (commission_template_id))
这里我们有魔力,因为Hibernate不创建表commission_template
。pgAdmin告诉我“没有这样的桌子”。最好的情况是,当我关闭应用程序和Hibernate调用时,它们会记录删除commission_template
表。
我试图更改表的名称,并将其设置为prowizje_szablony
.
Caused by: org.postgresql.util.PSQLException: ERROR: relation "prowizje_szablony" does not exist
Pozycja: 273
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2077)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1810)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:498)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:386)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:271)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
... 77 more
有什么想法,建议或暗示吗?
//编辑: IMO,问题在转换器的某个地方,Hibernate无法处理它们的表。
//解决办法:通过以下方式手动创建该表:
CREATE TABLE commissiontemplate
(
id bigint NOT NULL,
active boolean,
start timestamp without time zone,
"end" timestamp without time zone,
min double precision,
max double precision,
value double precision,
CONSTRAINT id PRIMARY KEY (id )
)
WITH (
OIDS=FALSE
);
ALTER TABLE commissiontemplate
OWNER TO koziolek;
默认名称(我知道波兰语是非常好的语言,您可以调用表chrząszcz
,但请.不是在IT中,而是将DDL更改为validate
不太好,因为我们需要更多的工具来覆盖模式更新,但是可以.
发布于 2014-12-04 09:38:31
答案是:
Hibernate不能使用名为end
的列创建表,因为它是关键字,但永远不会告诉您。只需记录事件“创建表”。
当我手动创建表时,pgAdmin将"
添加到DDL中,它可以正常工作。因为我有一些习惯,并且总是在查询中使用"
,所以手动检查查询正常。
当hibernate对这个表执行sql查询时,我得到SQLGrammarException
.这是个暗示是怎么回事。我更改了列名,现在工作正常。
发布于 2014-12-04 04:17:57
根据日志,Hibernate实际上是在为您创建这些表。确保连接到同一个数据库实例,并且使用的架构与Hibernate使用的模式相同。
您还可以尝试切换到:
<property name="hibernate.hbm2ddl.auto" value="update"/>
因为一旦SessionFactory关闭,create将删除模式。
https://stackoverflow.com/questions/27282536
复制