当我尝试持久化对象时,为什么我的@Id列没有生成?

时间:2016-08-25 18:56:48

标签: java spring jpa spring-boot spring-data

我有两个表,一个叫LoadMethod,另一个叫EventsWithComments。它们与外键Media_ID之间存在一对多的关系。 Media_ID也是加载方法表的主键,并且EventsWithComments表有一个名为EC_SK的主键,每当我持有一个LoadMethod对象时,它应该自动生成,该对象上有一个EventsWithComments数据对象列表。好。

以下是我的数据对象:

LoadMethodDO:

@Entity
@Table(name="LoadMethod")
public class LoadMethodDO {
//  @ManyToMany //TODO: dont forget about getters and setters
//  @JoinTable(name="LMClientDO")
//  private List<ClientDO> clientDoFk = new ArrayList<ClientDO>();

    @OneToMany(mappedBy="loadMethodDoFk", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    private List<EventsWithCommentsDO> eventsWithCommentsDO;

    @Id
    @Column(name="Media_ID")
    private String mediaId;
... 
}

EventsWithCommentsDO:

@Entity
@Table(name="LoadMethodComments")
public class EventsWithCommentsDO {
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name ="Media_ID", insertable=false,updatable=false)
private LoadMethodDO loadMethodDoFk;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@SequenceGenerator(name="sequence")

@Column(name="EC_SK")
private int ecSk;
@Column(name="Media_ID")
private String mediaId;
...
}

这是我试图运行的:( EventsWithCommentsDO构造函数初始化mediaId和注释列。另外,请注意我确保在loadmethod和带有注释的事件中都有相同的mediaId,以便关系起作用)

@SpringBootApplication
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner demo(LoadMethodRepository lm_repo, TBL_GroupRepository tblg_repo, EventsWithCommentsRepository ec_repo) {
        return (args) -> {

LoadMethodDO lmdo = new LoadMethodDO("1010101", "1", "d", "e", "w", "d");
            EventsWithCommentsDO ecdo = new EventsWithCommentsDO("1010101", "hello");
            lmdo.setEventsWithCommentsDO(ecdo);
            lm_repo.save(lmdo);
        };
    }

}

这是我的控制台输出:

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.6.RELEASE)

2016-08-25 11:46:16.406  INFO 68724 --- [           main] database.Application                     : Starting Application on LR90KLWJY with PID 68724 (C:\Users\vincni\git\consumer_load_method\bin started by vincni in C:\Users\vincni\git\consumer_load_method)
2016-08-25 11:46:16.408  INFO 68724 --- [           main] database.Application                     : No active profile set, falling back to default profiles: default
2016-08-25 11:46:16.446  INFO 68724 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@57f23557: startup date [Thu Aug 25 11:46:16 PDT 2016]; root of context hierarchy
2016-08-25 11:46:17.774  INFO 68724 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'lmPersistenceUnit'
2016-08-25 11:46:17.791  INFO 68724 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: lmPersistenceUnit
    ...]
2016-08-25 11:46:17.863  INFO 68724 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {4.3.11.Final}
2016-08-25 11:46:17.864  INFO 68724 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2016-08-25 11:46:17.865  INFO 68724 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2016-08-25 11:46:18.088  INFO 68724 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2016-08-25 11:46:18.735  WARN 68724 --- [           main] o.h.e.j.d.i.StandardDialectResolver      : HHH000385: Unknown Microsoft SQL Server major version [12] using SQL Server 2000 dialect
2016-08-25 11:46:18.742  INFO 68724 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
2016-08-25 11:46:18.901  INFO 68724 --- [           main] o.h.h.i.ast.ASTQueryTranslatorFactory    : HHH000397: Using ASTQueryTranslatorFactory
2016-08-25 11:46:19.115  INFO 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update
2016-08-25 11:46:19.115  INFO 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000102: Fetching database metadata
2016-08-25 11:46:19.116  INFO 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000396: Updating schema
2016-08-25 11:46:19.143  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000261: Table found: dbMOBClientTemp.dbo.Client
2016-08-25 11:46:19.143  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000037: Columns: [expndedidind, isd_lupd_ts, client_id, isd_lupd_id]
2016-08-25 11:46:19.143  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000108: Foreign keys: []
2016-08-25 11:46:19.143  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000126: Indexes: [pxclient]
2016-08-25 11:46:19.149  INFO 68724 --- [           main] java.sql.DatabaseMetaData                : HHH000262: Table not found: Client_LoadMethod
2016-08-25 11:46:19.158  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000261: Table found: dbMOBClientTemp.dbo.LoadMethod
2016-08-25 11:46:19.158  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000037: Columns: [actind, loadmthdcd, dtltransrcdind, media_id, acctmngdind, formatmapcd, isd_lupd_ts, isd_lupd_id]
2016-08-25 11:46:19.158  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000108: Foreign keys: []
2016-08-25 11:46:19.158  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000126: Indexes: [pxldmthd]
2016-08-25 11:46:19.166  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000261: Table found: dbMOBClientTemp.dbo.LoadMethodClient
2016-08-25 11:46:19.166  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000037: Columns: [media_id, isd_lupd_ts, client_id, isd_lupd_id]
2016-08-25 11:46:19.166  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000108: Foreign keys: [rcmedialm, rcclientlm]
2016-08-25 11:46:19.166  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000126: Indexes: [pxlmc]
2016-08-25 11:46:19.172  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000261: Table found: dbMOBClientTemp.dbo.LoadMethodComments
2016-08-25 11:46:19.173  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000037: Columns: [ec_sk, media_id, cmt, isd_lupd_ts, isd_lupd_id]
2016-08-25 11:46:19.173  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000108: Foreign keys: [rcmediaid]
2016-08-25 11:46:19.173  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000126: Indexes: [pxlmcmt]
2016-08-25 11:46:19.177  INFO 68724 --- [           main] java.sql.DatabaseMetaData                : HHH000262: Table not found: Client_LoadMethod
2016-08-25 11:46:19.180  INFO 68724 --- [           main] java.sql.DatabaseMetaData                : HHH000262: Table not found: Client_LoadMethod
2016-08-25 11:46:19.183 ERROR 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000388: Unsuccessful: create table Client_LoadMethod (Client_Client_ID varchar(255) not null, loadMethodDoFk_Media_ID varchar(255) not null)
2016-08-25 11:46:19.183 ERROR 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : CREATE TABLE permission denied in database 'dbMOBClientTemp'.
2016-08-25 11:46:19.184 ERROR 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000388: Unsuccessful: alter table Client_LoadMethod add constraint FK_4xvrb94fq2i375kwbxdj8wcgn foreign key (loadMethodDoFk_Media_ID) references LoadMethod
2016-08-25 11:46:19.184 ERROR 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : Cannot find the object "Client_LoadMethod" because it does not exist or you do not have permissions.
2016-08-25 11:46:19.185 ERROR 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000388: Unsuccessful: alter table Client_LoadMethod add constraint FK_t9lg6e21ci1307rp047xxr2d5 foreign key (Client_Client_ID) references Client
2016-08-25 11:46:19.185 ERROR 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : Cannot find the object "Client_LoadMethod" because it does not exist or you do not have permissions.
2016-08-25 11:46:19.185  INFO 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000232: Schema update complete
2016-08-25 11:46:19.232  INFO 68724 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'mtsPersistentUnit'
2016-08-25 11:46:19.232  INFO 68724 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: mtsPersistentUnit
    ...]
2016-08-25 11:46:19.412  INFO 68724 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect
2016-08-25 11:46:19.418  INFO 68724 --- [           main] o.h.h.i.ast.ASTQueryTranslatorFactory    : HHH000397: Using ASTQueryTranslatorFactory
2016-08-25 11:46:19.436  INFO 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update
2016-08-25 11:46:19.436  INFO 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000102: Fetching database metadata
2016-08-25 11:46:19.436  INFO 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000396: Updating schema
2016-08-25 11:46:19.464  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000261: Table found: dbMTS.dbo.tblEVRPROF
2016-08-25 11:46:19.464  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000037: Columns: [contact_4_sw, omit_all_rejects, int_contact_1_sw, spec_hand_code4, spec_hand_code3, detailtran_deptrm, spec_hand_code2, spec_hand_code1, who_entered, spec_hand_code8, spec_hand_code7, spec_hand_code6, spec_hand_code5, last_load, date_entered, id, omit_reject_30, contact_3_sw, groups_div, omit_reject_08, omit_reject_09, omit_reject_06, omit_reject_07, detailtran_mbrtrm, omit_reject_01, omit_reject_04, detailtran_mbrupd, detailtran_depupd, omit_reject_05, omit_reject_02, omit_reject_03, int_contact_3_sw, time_entered, detailtran_mbradd, detailtran_depadd, date_updated, omit_reject_19, omit_reject_17, omit_reject_18, distribution, omit_reject_11, omit_reject_12, omit_reject_10, omit_reject_15, omit_reject_16, omit_reject_13, omit_reject_14, contact_2_sw, who_updated, int_contact_2_sw, omit_reject_28, omit_reject_29, time_updated, omit_reject_22, fk_group_id, omit_reject_23, omit_reject_20, omit_reject_21, omit_reject_26, send_evr, omit_reject_27, omit_reject_24, omit_reject_25, contact_1_sw]
2016-08-25 11:46:19.464  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000108: Foreign keys: []
2016-08-25 11:46:19.465  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000126: Indexes: [pxevrprof]
2016-08-25 11:46:19.486  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000261: Table found: dbMTS.dbo.tblGROUP
2016-08-25 11:46:19.486  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000037: Columns: [date_updated, penalty_ind, media_count, y2k_advised, download_full, label_spec, who_entered, group_inactive, report_preference, edi_type, prod_test_ind, late_status, frequency, g_tech_num, media_type, control_number, hot_loadt, media_id, date_entered, id, mit_id, emc_synch_flag, who_updated, block_size, strategic_group, report_processor, format_program, gmisc_flag_2, gmisc_flag_1, gmisc_flag_3, record_size, four_digit_year, respon_anal, time_updated, open_enroll_mnth, use_tpa_contact, ol_id, fk_tpa_id, zeke_event, name, time_entered, package_name, group_maint_ind, elig_password, edt_advised, need_labels, spanned_zipfile]
2016-08-25 11:46:19.486  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000108: Foreign keys: [rcgrpadmin]
2016-08-25 11:46:19.486  INFO 68724 --- [           main] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000126: Indexes: [pxgroup]
2016-08-25 11:46:19.488 ERROR 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000388: Unsuccessful: alter table tblEVRPROF add constraint FK_db3d8l7o24v38y79714cm0wmn foreign key (FK_GROUP_ID) references tblGROUP
2016-08-25 11:46:19.488 ERROR 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : Cannot find the object "tblEVRPROF" because it does not exist or you do not have permissions.
2016-08-25 11:46:19.488  INFO 68724 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000232: Schema update complete
2016-08-25 11:46:20.088  INFO 68724 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
Hibernate: select loadmethod0_.Media_ID as Media_ID1_2_1_, loadmethod0_.AcctMngdInd as AcctMngd2_2_1_, loadmethod0_.ActInd as ActInd3_2_1_, loadmethod0_.DtlTransRcdInd as DtlTrans4_2_1_, loadmethod0_.FormatMapCd as FormatMa5_2_1_, loadmethod0_.ISD_LUPD_ID as ISD_LUPD6_2_1_, loadmethod0_.ISD_LUPD_TS as ISD_LUPD7_2_1_, loadmethod0_.LoadMthdCd as LoadMthd8_2_1_, eventswith1_.Media_ID as Media_ID5_2_3_, eventswith1_.EC_SK as EC_SK1_4_3_, eventswith1_.EC_SK as EC_SK1_4_0_, eventswith1_.Cmt as Cmt2_4_0_, eventswith1_.ISD_LUPD_ID as ISD_LUPD3_4_0_, eventswith1_.ISD_LUPD_TS as ISD_LUPD4_4_0_, eventswith1_.Media_ID as Media_ID5_4_0_ from LoadMethod loadmethod0_ left outer join LoadMethodComments eventswith1_ on loadmethod0_.Media_ID=eventswith1_.Media_ID where loadmethod0_.Media_ID=?
Hibernate: insert into LoadMethod (AcctMngdInd, ActInd, DtlTransRcdInd, FormatMapCd, ISD_LUPD_ID, ISD_LUPD_TS, LoadMthdCd, Media_ID) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into LoadMethodComments (Cmt, ISD_LUPD_ID, ISD_LUPD_TS, Media_ID) values (?, ?, ?, ?)
2016-08-25 11:46:20.214  WARN 68724 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 515, SQLState: 23000
2016-08-25 11:46:20.214 ERROR 68724 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Cannot insert the value NULL into column 'EC_SK', table 'dbMOBClientTemp.dbo.LoadMethodComments'; column does not allow nulls. INSERT fails.
2016-08-25 11:46:20.216  WARN 68724 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Warning Code: 3621, SQLState: S0000
2016-08-25 11:46:20.216  WARN 68724 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : The statement has been terminated.
2016-08-25 11:46:20.253 ERROR 68724 --- [           main] o.s.boot.SpringApplication               : Application startup failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:803) [spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784) [spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:771) [spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
    at database.Application.main(Application.java:26) [bin/:na]
Caused by: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:259) ~[spring-orm-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225) ~[spring-orm-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:436) ~[spring-orm-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at com.sun.proxy.$Proxy60.save(Unknown Source) ~[na:na]
    at database.Application.lambda$0(Application.java:71) [bin/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE]
    ... 6 common frames omitted

感兴趣的控制台输出是说Cannot insert the value NULL into column 'EC_SK',的部分。我尝试添加@sequenceGenerator注释,但是无论是否有错误都没有改变。我认为有些可疑事实是insertable=false, updatable=false)注释@joincolumneventswithcommentsDO注释Media_ID。但是,如果我把它拿出来,它说我有一个重复的字段@with_syms func(a, b) ,但我想在两个对象中都有一个mediaId,这是我碰巧在网上找到的唯一工作。

1 个答案:

答案 0 :(得分:2)

@GeneratedValue(strategy = GenerationType.AUTO)添加到您的ID变量中。它会在持久保存对象时自动创建唯一ID。

最好的问候:)