使用spring数据jpa和spring boot问题

时间:2017-02-13 01:13:56

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

我的实体客户

@Entity
public class Client implements Serializable {
@Id @GeneratedValue
private long code;
private  String nom;
private  String email;
@OneToMany(mappedBy = "client",fetch = FetchType.LAZY)
private Collection<Compte> comptes;
public Client(long code, String nom, String email, Collection<Compte> comptes) {
    this.code = code;
    this.nom = nom;
    this.email = email;
    this.comptes = comptes;
}
/...
getters and setters
}

entity Compte

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE_CPTE,",discriminatorType= DiscriminatorType.STRING,length = 2)
public abstract class Compte implements Serializable {
@Id
private String codeCompte;
private Date dataCreation;
private  double solde;
@ManyToOne
@JoinColumn(name="CODE_CLI")
private  Client client;
@OneToMany(mappedBy="compte")
private Collection<Operation> operations;

public Compte() {
    super();
}

      public Compte(String codeCompte, Date dataCreation, double solde, Client          client, Collection<Operation> operations) {
       this.codeCompte = codeCompte;
       this.dataCreation = dataCreation;
       this.solde = solde;
       this.client = client;
       this.operations = operations;
       }

    /...
   getters and setters
  }

实体操作

   @Entity
   @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
   @DiscriminatorColumn(name = "TYPE_OP",discriminatorType = DiscriminatorType.STRING,length = 1)
   public abstract class Operation implements Serializable{
   @Id @GeneratedValue
   private long numero;
   private Date dateOperation;
   private double montant;
   @ManyToOne
   @JoinColumn(name = "CODE_CPTE")
   private Compte compte;

   public Operation() {
    super();
    }

   public Operation(Date dateOperation, double montant, Compte compte) {
    this.dateOperation = dateOperation;
    this.montant = montant;
    this.compte = compte;
   }
  /...getters and setters 
  }

实体退休人员

  @Entity
  @DiscriminatorValue("R")
  public class Retrait extends Operation {
  public Retrait() {
  }
  public Retrait(Date dateOperation, double montant, Compte compte) {
    super(dateOperation, montant, compte);
  }
  }

实体经文

  @Entity
  @DiscriminatorValue("V")
  public class Versement extends Operation {
  public Versement() {
  }
  public Versement(Date dateOperation, double montant, Compte compte) {
    super(dateOperation, montant, compte);
  }
  }

实体Comptecourant

@Entity
@DiscriminatorValue("CE")
public class CompteEpargne extends Compte {
private double taux;
public CompteEpargne(String s, Date date, int i, Client c2, double taux) {
    this.taux = taux;
}

public CompteEpargne(String codeCompte, Date dataCreation, double solde, Client client, Collection<Operation> operations, double taux) {
    super(codeCompte, dataCreation, solde, client, operations);
    this.taux = taux;
}
public CompteEpargne(){
    super();
}
/...getters and setters 
}
**entity CompteEpagne**

@Entity
@DiscriminatorValue("CC")
public class CompteCourant extends Compte {
private  double decouvert;

public CompteCourant(String s, Date date, int i, Client c1, double decouvert) {
    this.decouvert = decouvert;
}

public CompteCourant(String codeCompte, Date dataCreation, double solde, Client client, Collection<Operation> operations, double decouvert) {
    super(codeCompte, dataCreation, solde, client, operations);
    this.decouvert = decouvert;
}
public  CompteCourant(){
    super();
}
/...getters and setters 
}

Repositorys 1

@Repository
public interface ClientRep extends JpaRepository<Client,Long> {
}

2

@Repository
public interface CompteRep extends JpaRepository<Compte,String> {
}

第3

@Repository
public interface OperationRep extends JpaRepository<Operation,Long> {
@Query("select o from Operation o where o.compte.codeCompte=:x order by o.dateOperation desc ")
public Page<Operation> listOperation(String codeCpte, Pageable pageable);
}

服务

public interface IBankMetier  {
public Compte consulterCompte(String codeCpte);
public void verser(String codeCpte, double montant);
public void retirer(String codeCpte, double montant);
public void virement(String codeCpte1, String codeCpte2, double montant);
public Page<Operation> listOperation(String codeCpte, int page, int size);
}

implimantation

@Service
@Transactional
public class BankMetierImp implements IBankMetier {
@Autowired
private CompteRep compteRep;
@Autowired
private OperationRep operationRep;
@Override
public Compte consulterCompte(String codeCpte) {
    Compte cp = compteRep.findOne(codeCpte);
    if (cp == null) throw new RuntimeException("Compte introvable");
    return cp;
}

@Override
public void verser(String codeCpte, double montant) {
    Compte cp = consulterCompte(codeCpte);
    Versement v = new Versement(new Date(), montant, cp);
    operationRep.save(v);
    cp.setSolde((cp.getSolde() + montant));
    compteRep.save(cp);
}

@Override
public void retirer(String codeCpte, double montant) {
    Compte cp = consulterCompte(codeCpte);
    double facili = 0;
    if (cp instanceof CompteCourant)
        facili = ((CompteCourant) cp).getDecouvert();
    if (cp.getSolde() + facili < montant)
        throw new RuntimeException("solde insuffisant");
    Retrait retrait = new Retrait(new Date(), montant, cp);
    operationRep.save(retrait);
    cp.setSolde((cp.getSolde() - montant));
    compteRep.save(cp);
}

@Override
public void virement(String codeCpte1, String codeCpte2, double montant) {
    retirer(codeCpte1, montant);
    verser(codeCpte2, montant);
}

@Override
public Page<Operation> listOperation(String codeCpte, int page, int size) {
    return operationRep.listOperation(codeCpte,new PageRequest(page,size));
}
}

Spring boot Application

@SpringBootApplication
public class MeinproApplication implements CommandLineRunner {
@Autowired
private ClientRep clientRep;
@Autowired
private CompteRep compteRep;
@Autowired
private OperationRep operationRep;
@Autowired
BankMetierImp bankMetier;
public static void main(String[] args) {
    SpringApplication.run(MeinproApplication.class, args);

}
  @Override
  public void run(String... strings) throws Exception {
    Client c1=clientRep.save(new Client("martin","martin@gmail.com"));
    Client c2=clientRep.save(new Client("john","john@gmail.com"));

    Compte cp1=compteRep.save(new CompteCourant("c1",new Date(),9000,c1,6000));
    Compte cp2=compteRep.save(new CompteEpargne("c2",new Date(),63000,c2,12220));

    operationRep.save(new Retrait(new Date(),6000,cp1));
    operationRep.save(new Retrait(new Date(),10000,cp2));
    operationRep.save(new Versement(new Date(),2000,cp1));
    operationRep.save(new Versement(new Date(),20,cp2));
    bankMetier.verser("c1",1000);
    bankMetier.virement("c1","c2",2000);

}
}

application.properties

# DataSource settings:
spring.datasource.url=jdbc:mysql://localhost:3306/monbank
spring.datasource.username=roo
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop          
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

当我运行它时booom !!!!

2017-02-12 16:15:24.858  INFO 6232 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.11.Final}
2017-02-12 16:15:24.860  INFO 6232 --- [  restartedMain] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2017-02-12 16:15:24.863  INFO 6232 --- [  restartedMain] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2017-02-12 16:15:24.947  INFO 6232 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-02-12 16:15:25.113  INFO 6232 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2017-02-12 16:15:25.938  INFO 6232 --- [  restartedMain] org.hibernate.tuple.PojoInstantiator     : HHH000182: No default (no-argument) constructor for class: meinpro.entiti.Client (class must be instantiated by Interceptor)
2017-02-12 16:15:26.209  INFO 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
Hibernate: alter table compte drop foreign key FK2hw4shqsxc782lychpkr52lmv
2017-02-12 16:15:26.228 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table compte drop foreign key FK2hw4shqsxc782lychpkr52lmv
2017-02-12 16:15:26.228 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : Table 'monbank.compte' doesn't exist
Hibernate: alter table operation drop foreign key FKkr9nfjf0ipqrw5xwcf9jqq6gv
2017-02-12 16:15:26.229 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table operation drop foreign key FKkr9nfjf0ipqrw5xwcf9jqq6gv
2017-02-12 16:15:26.229 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : Table 'monbank.operation' doesn't exist
Hibernate: drop table if exists client
Hibernate: drop table if exists compte
Hibernate: drop table if exists operation
Hibernate: create table client (code bigint not null auto_increment, email varchar(255), nom varchar(255), primary key (code))
Hibernate: create table compte (type_cpte, varchar(2) not null, code_compte varchar(255) not null, data_creation datetime, solde double precision not null, decouvert double precision, taux double precision, code_cli bigint, primary key (code_compte))
2017-02-12 16:15:26.712 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: create table compte (type_cpte, varchar(2) not null, code_compte varchar(255) not null, data_creation datetime, solde double precision not null, decouvert double precision, taux double precision, code_cli bigint, primary key (code_compte))
2017-02-12 16:15:26.713 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' varchar(2) not null, code_compte varchar(255) not null, data_creation datetime,' at line 1
Hibernate: create table operation (type_op varchar(1) not null, numero bigint not null auto_increment, date_operation datetime, montant double precision not null, code_cpte varchar(255), primary key (numero))
Hibernate: alter table compte add constraint FK2hw4shqsxc782lychpkr52lmv foreign key (code_cli) references client (code)
2017-02-12 16:15:27.146 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table compte add constraint FK2hw4shqsxc782lychpkr52lmv foreign key (code_cli) references client (code)
2017-02-12 16:15:27.147 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : Table 'monbank.compte' doesn't exist
Hibernate: alter table operation add constraint FKkr9nfjf0ipqrw5xwcf9jqq6gv foreign key (code_cpte) references compte (code_compte)
2017-02-12 16:15:27.330 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table operation add constraint FKkr9nfjf0ipqrw5xwcf9jqq6gv foreign key (code_cpte) references compte (code_compte)
2017-02-12 16:15:27.331 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : Can't create table `monbank`.`#sql-23d4_77` (errno: 150 "Foreign key constraint is incorrectly formed")
2017-02-12 16:15:27.332  INFO 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
2017-02-12 16:15:27.414  INFO 6232 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-02-12 16:15:27.938  INFO 6232 --- [  restartedMain] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2017-02-12 16:15:28.776  INFO 6232 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2ffb82a2: startup date [Sun Feb 12 16:15:18 PST 2017]; root of context hierarchy
2017-02-12 16:15:28.936  INFO 6232 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-02-12 16:15:28.938  INFO 6232 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-02-12 16:15:29.022  INFO 6232 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-12 16:15:29.022  INFO 6232 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-12 16:15:29.112  INFO 6232 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-12 16:15:29.263  WARN 6232 --- [  restartedMain] .t.AbstractTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2017-02-12 16:15:30.063  INFO 6232 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2017-02-12 16:15:30.159  INFO 6232 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-02-12 16:15:30.246  INFO 6232 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
Hibernate: insert into client (email, nom) values (?, ?)
Hibernate: insert into client (email, nom) values (?, ?)
2017-02-12 16:15:30.461  INFO 6232 --- [  restartedMain] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-02-12 16:15:30.472 ERROR 6232 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    at meinpro.MeinproApplication.main(MeinproApplication.java:30) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.1.RELEASE.jar:1.5.1.RELEASE]
Caused by: org.springframework.orm.jpa.JpaSystemException: ids for this class must be manually assigned before calling save(): meinpro.entiti.Compte; nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): meinpro.entiti.Compte
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) ~[spring-data-jpa-1.11.0.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at com.sun.proxy.$Proxy86.save(Unknown Source) ~[na:na]
    at meinpro.MeinproApplication.run(MeinproApplication.java:38) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
    ... 11 common frames omitted
Caused by: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): meinpro.entiti.Compte
    at org.hibernate.id.Assigned.generate(Assigned.java:34) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at com.sun.proxy.$Proxy82.persist(Unknown Source) ~[na:na]
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:508) ~[spring-data-jpa-1.11.0.RELEASE.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    ... 22 common frames omitted

2017-02-12 16:15:30.478  INFO 6232 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2ffb82a2: startup date [Sun Feb 12 16:15:18 PST 2017]; root of context hierarchy
2017-02-12 16:15:30.483  INFO 6232 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2017-02-12 16:15:30.485  INFO 6232 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-02-12 16:15:30.485  INFO 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
Hibernate: alter table compte drop foreign key FK2hw4shqsxc782lychpkr52lmv
2017-02-12 16:15:30.488 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table compte drop foreign key FK2hw4shqsxc782lychpkr52lmv
2017-02-12 16:15:30.489 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : Table 'monbank.compte' doesn't exist
Hibernate: alter table operation drop foreign key FKkr9nfjf0ipqrw5xwcf9jqq6gv
2017-02-12 16:15:30.499 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table operation drop foreign key FKkr9nfjf0ipqrw5xwcf9jqq6gv
2017-02-12 16:15:30.500 ERROR 6232 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : Can't DROP 'FKkr9nfjf0ipqrw5xwcf9jqq6gv'; check that column/key exists
Hibernate: drop table if exists client
      Hibernate: drop table if exists compte
      Hibernate: drop table if exists operation
      2017-02-12 16:15:30.879  INFO 6232 --- [  restartedMain]            org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
      Process finished with exit code 0

请帮帮我

3 个答案:

答案 0 :(得分:1)

错误消息告诉您确切的问题

  

引起:org.hibernate.id.IdentifierGenerationException:在调用save()之前必须手动分配此类的ID:meinpro.entiti.Compte

Compte类中的id不是生成的值,就像其他表

一样
@Id
private String codeCompte;

因此您需要在保存之前自己设置此值。

现在查看CompteEpargneCompteCourant(扩展Compte)构造函数

public CompteEpargne(String s, Date date, int i, Client c2, double taux) {
    this.taux = taux;
}

public CompteEpargne(String codeCompte, Date dataCreation, double solde, Client client, Collection<Operation> operations, double taux) {
    super(codeCompte, dataCreation, solde, client, operations);
    this.taux = taux;
}

public CompteCourant(String s, Date date, int i, Client c1, double decouvert) {
    this.decouvert = decouvert;
}

public CompteCourant(String codeCompte, Date dataCreation, double solde, Client client, Collection<Operation> operations, double decouvert) {
    super(codeCompte, dataCreation, solde, client, operations);
    this.decouvert = decouvert;
}

每种类型的第一个构造函数只设置一个值,而第二个构造函数调用超级构造函数,它将 all 设置为值(包括id)。保存时,您正在使用第一个构造函数

Compte cp1=compteRep.save(new CompteCourant("c1",new Date(),9000,c1,6000));
Compte cp2=compteRep.save(new CompteEpargne("c2",new Date(),63000,c2,12220));

您正在调用仅设置一个值的构造函数。所以你可能需要修复那些构造函数如何设置属性。也许你想做类似

的事情
public CompteEpargne(String s, Date date, int i, Client c2, double taux) {
    this(s, date, i, c2, new ArrayList<Operation>(), taux);
}

你可以调用其他构造函数。

答案 1 :(得分:0)

简单解决方案

  1. 您的id属性未设置。这可能是因为DB字段未设置为自动递增。
  2. 对于MySQL(正如您在“属性”文件中所提到的),请使用以下示例来定义您的POJO类。

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id",   unique=true, nullable=false) public Long getId() { return this.id; } 
    

答案 2 :(得分:0)

感谢朋友,问题出在Compte类

 @DiscriminatorColumn(name = "TYPE_CPTE,",discriminatorType= DiscriminatorType.STRING,length = 2)

在名称中= = strong>&#34; TYPE_CPTE,&#34; hhhh我在TYPE_CPTE之后看不到逗号

@DiscriminatorColumn(name = "TYPE_CPTE",discriminatorType= DiscriminatorType.STRING,length = 2)