繁体   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