2017-02-13 186 views
0

我的實體客戶端採用帶彈簧的引導問題

@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 
@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 
    } 

彈簧數據的JPA 實體Retirer

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

實體versement

@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

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

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

@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)); 
} 
} 

春季啓動應用

@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","[email protected]")); 
    Client c2=clientRep.save(new Client("john","[email protected]")); 

    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[email protected]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[email protected]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 

請幫我

+1

如果您有任何關於您在此提供的代碼和日誌,請解釋您的問題。只有代碼不能解釋任何東西。 – Jeet

回答

1

該錯誤消息告訴你到底是什麼問題

造成的:org.hibernate.id.IdentifierGenerationException:此類ID必須在調用save()之前手動分配:meinpro.entiti。孔特

的ID在你Compte類不是一個生成的值,像其他表

@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; 
} 

每種類型的第一個構造只設置一個值,而第二個調用超構造函數,它設置所有的值(包含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); 
} 

你在哪裏調用其他構造函數。

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; } 
    
+0

我在哪裏可以實現? – Stefan

+0

謝謝,但它仍然無法正常工作 – Stefan

0

感謝網友的問題是在類孔特

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

在名爲= 「TYPE_CPTE,」 HHHH我不看後面的逗號TYPE_CPTE

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