我正在使用Liquibase为我的春季启动项目创建数据库。当我尝试启动PostgreSQL时,我会遇到无法追踪的错误。我有以下实体:
@Entity
public class SubscriptionPackage {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long subscriptionPackageId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "subscriptionPackage", fetch = FetchType.EAGER)
private List<Menu> menus = new ArrayList<Menu>();
@Enumerated(EnumType.STRING)
@Column(name = "servicePackage", nullable = false)
private ServicePackageType servicePackage;
private String description;
和
@Entity
public class Menu {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long menuId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "menu", fetch = FetchType.EAGER)
private List<MenuItem> menuItems = new ArrayList<MenuItem>();
@ManyToOne
@JoinColumn(name = "subscriptionPackageId", nullable = false)
private SubscriptionPackage subscriptionPackage;
private String displayText;
private int displayOrder;
然后在我的db.changelog-master.xml中,我有:
<changeSet id="1" ..>
<createTable tableName="SubscriptionPackage">
<column name="subscriptionPackageId" type="bigint" autoIncrement="true">
<constraints primaryKey="true"/>
</column>
<column name="servicePackage" type="varchar(300)"/>
<column name="description" type="varchar(500)"/>
</createTable>
..
<createTable tableName="Menu">
<column name="menuId" type="bigint" autoIncrement="true">
<constraints primaryKey="true"/>
</column>
<column name="subscriptionPackageId" type="bigint"/>
<column name="displayText" type="varchar(300)"/>
<column name="displayOrder" type="int"/>
</createTable>
</changeSet>
<changeSet id="2" ..>
<addForeignKeyConstraint baseColumnNames="subscriptionPackageId"
baseTableName="SubscriptionPackage"
constraintName="fk_menu_subscriptionpackage"
onDelete="CASCADE"
onUpdate="RESTRICT"
referencedColumnNames="menuId"
referencedTableName="Menu"/>
</changeSet>
这是我得到的错误:
Caused by: org.postgresql.util.PSQLException: ERROR: relation "subscription_package" does not exist
Position: 13
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
我不明白为什么找不到"subscription_package"关系。我以为这是外键,但在我的第二个变更集上添加了一个显式创建。表是在数据库中创建的,所以我认为映射是正确的,我只是不知道是什么导致了这个特定的问题。
更新我已经确认数据库表正在创建中,并且外键约束已经在Postgresql:中就位
CREATE TABLE "SubscriptionPackage"
(
"subscriptionPackageId" bigserial NOT NULL,
"servicePackage" character varying(300),
description character varying(500),
CONSTRAINT pk_subscriptionpackage PRIMARY KEY ("subscriptionPackageId"),
CONSTRAINT fk_menu_subscriptionpackage FOREIGN KEY ("subscriptionPackageId")
REFERENCES "Menu" ("menuId") MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE CASCADE
)
当我试图保存一个没有关联Menu的SubscriptionPackage列表时,我已经追踪到了在我的启动/种子中抛出的异常。现在,我想知道我的SubscriptionPackage是否有菜单列表,而我的菜单是否有对SubscriptionPackage的引用,是因为我的订阅变得混乱了。
无论如何,我要结束这件事,问一个不同的问题,因为我很确定这不再是一个利口酒的问题。
关闭
我认为您缺少将默认模式名称添加到spring-hibernate配置中。
这里也有类似的情况