簡體   English   中英

與Hibernate和JPA進行Spring集成-一個表在啟動時未創建,其他表

[英]Spring Integration with Hibernate & JPA - one table not created on boot , others are

我試圖將兩個額外的表添加到我現有的Spring應用程序中。 一個正在數據庫中創建,而另一個則沒有。 我看不到JPA對象有任何明顯的區別,並且我已經更新了這兩個對象的數據庫屬性。 這些表之間的唯一主要區別是,一個表以雙向關系映射到用戶表,另一個僅是單向的。

以下是一些代碼示例:

persistence.xml:

<persistence-unit name="samplePersistenceUnit" transaction-type="RESOURCE_LOCAL">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.bpc.services.domain.objects.User</class>
    <class>com.bpc.services.domain.objects.Account</class>
    <class>com.bpc.services.domain.objects.Transaction</class>
    <class>com.bpc.services.domain.objects.Payment</class>
    <class>com.bpc.services.domain.objects.Product</class>
   <properties>
       <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
       <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
       <property name="hibernate.connection.charSet" value="UTF-8"/>
       <property name="hbm2ddl.auto" value="update"/>
       <property name="show_sql" value="true"/>
       <property name="hibernate.hbm2ddl.auto" value="update"/>
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
   </properties>

data-context.xml JPA聲明:

<jpa:repositories base-package="com.bpc.services.domain.repositories" />

產品實體:

@Entity
@Table(name = "product")
public class Product extends BaseEntity {

付款實體:

@Entity
@Table(name = "payment")
public class Payment extends BaseEntity {
    @ManyToOne
    User user;

用戶實體:

@Entity
@Table(name="rest_user")
public class User extends BaseEntity {
    @OneToMany(mappedBy="user",
        targetEntity=Payment.class,
        cascade= CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Payment> payments = new ArrayList<Payment>();

帳戶實體:

@Entity
@Table(name = "rest_account")
public class Account extends BaseEntity {
    @ManyToOne
    private Product appliedProduct;

產品表已創建,並以*:1關系鏈接到Account對象。 但是我的付款表不見了。 部署運行良好,日志中顯示以下內容:

[localhost-startStop-1] DEBUG org.hibernate.cfg.Ejb3Column-綁定列:Ejb3Column {table = org.hibernate.mapping.Table(payment),mappingColumn = id,insertable = true,updatable = true,unique = false}

因此,該應用程序知道Payment域對象並在部署中使用它,但是當我嘗試通過客戶端使用服務時,日志顯示如下:

調試ohejdbc.spi.SqlExceptionHelper-無法提取ResultSet [n / a] com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'services.payment'不存在

[http-bio-8080-exec-3]警告ohejdbc.spi.SqlExceptionHelper-SQL錯誤:1146,SQLState:42S02

[http-bio-8080-exec-3]錯誤ohejdbc.spi.SqlExceptionHelper-表'services.payment'不存在


UPDATE我嘗試使用“創建”而不是“更新”運行(如答案中所建議),但是存在相同的問題。 現在有一個與付款表相關的附加日志條目:

11:37:14.994 [localhost-startStop-1]調試osjdLazyConnectionDataSourceProxy-連接到數據庫以進行操作'createStatement'

11:37:14.999 [localhost-startStop-1]調試org.hibernate.SQL-更改表付款刪除外鍵FK_5b79940uennr1ffusdus7cp2r

11:37:15.012 [localhost-startStop-1]錯誤ohtool.hbm2ddl.SchemaExport-HHH000389:不成功:更改表付款刪除外鍵FK_5b79940uennr1ffusdus7cp2r

11:37:15.012 [localhost-startStop-1]錯誤ohtool.hbm2ddl.SchemaExport-表'services.payment'不存在

11:37:15.012 [localhost-startStop-1]調試org.hibernate.SQL-更改表rest_account刪除外鍵FK_ek67yy1rmivvpoofrc0603du9

11:37:15.513 [localhost-startStop-1]調試org.hibernate.SQL-更改表rest_verification_token刪除外鍵FK_9i1lxa0i6h09fcobtm570hq7u

11:37:16.000 [localhost-startStop-1]調試org.hibernate.SQL-更改表事務刪除外鍵FK_8i8qo3qvlyg4xaiqgrnbpfvvh

11:37:16.498 [localhost-startStop-1]調試org.hibernate.SQL-更改表事務刪除外鍵FK_ce9ag0mlblwcp5n1bi1f2xwgs

11:37:16.993 [localhost-startStop-1]調試org.hibernate.SQL-如果存在付款則刪除表

加載付款存儲庫時(無任何例外),這將顯示在日志中:

身份插入:插入付款(time_created,uuid,version,description,in,out,performed_by,user,user_id)值(?,?,?,?,?,?,?,?,?)

11:37:14.919 [localhost-startStop-1]調試ohlpbispaces.QuerySpacesImpl-添加QuerySpace:uid =-> org.hibernate.loader.plan.build.internal.spaces.EntityQuerySpaceImpl@b90c767]

11:37:14.923 [localhost-startStop-1]調試ohpwspi.MetamodelGraphWalker-訪問屬性路徑:timeCreated

11:37:14.923 [localhost-startStop-1]調試ohpwspi.MetamodelGraphWalker-訪問屬性路徑:uuid

11:37:14.923 [localhost-startStop-1]調試ohpwspi.MetamodelGraphWalker-訪問屬性路徑:版本

11:37:14.923 [localhost-startStop-1]調試ohpwspi.MetamodelGraphWalker-訪問屬性路徑:描述

11:37:14.923 [localhost-startStop-1]調試ohpwspi.MetamodelGraphWalker-訪問屬性路徑:

11:37:14.923 [localhost-startStop-1]調試ohpwspi.MetamodelGraphWalker-訪問屬性路徑:out

11:37:14.923 [localhost-startStop-1]調試ohpwspi.MetamodelGraphWalker-訪問屬性路徑:performBy

11:37:14.923 [localhost-startStop-1]調試ohpwspi.MetamodelGraphWalker-訪問屬性路徑:用戶

11:37:14.923 [localhost-startStop-1]調試ohlpbispaces.QuerySpacesImpl-添加QuerySpace:uid =-> org.hibernate.loader.plan.build.internal.spaces.EntityQuerySpaceImpl@74febc11]

您是否嘗試過<property name="hibernate.hbm2ddl.auto" value="create"/>而不是update嗎? 請注意, create正在破壞先前的數據! 有關詳細信息,請參閱: doc

我在JPA對象中使用了一個變量名,它也是一個SQL關鍵字

將“ in”更改為“ money_in”,並創建了我的表格

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM