java - ConstraintViolationException: Validation failed for classes [....entities.WalletInfo] during persist -


i work spring mvc restful app , constraintviolationexception while persisting. error message provided below,

exception in thread " starting" javax.validation.constraintviolationexception: validation failed classes [mobi.puut.entities.walletinfo] during persist time groups [javax.validation.groups.default, ] list of constraint violations:[     constraintviolationimpl{interpolatedmessage='may not null', propertypath=id, rootbeanclass=class mobi.puut.entities.walletinfo, messagetemplate='{javax.validation.constraints.notnull.message}'}     constraintviolationimpl{interpolatedmessage='may not null', propertypath=currency, rootbeanclass=class mobi.puut.entities.walletinfo, messagetemplate='{javax.validation.constraints.notnull.message}'} ]     @ org.hibernate.cfg.beanvalidation.beanvalidationeventlistener.validate(beanvalidationeventlistener.java:140)     @ org.hibernate.cfg.beanvalidation.beanvalidationeventlistener.onpreinsert(beanvalidationeventlistener.java:80)     @ org.hibernate.action.internal.entityidentityinsertaction.preinsert(entityidentityinsertaction.java:197)     @ org.hibernate.action.internal.entityidentityinsertaction.execute(entityidentityinsertaction.java:75)     @ org.hibernate.engine.spi.actionqueue.execute(actionqueue.java:626)     @ org.hibernate.engine.spi.actionqueue.addresolvedentityinsertaction(actionqueue.java:280)     @ org.hibernate.engine.spi.actionqueue.addinsertaction(actionqueue.java:261)     @ org.hibernate.engine.spi.actionqueue.addaction(actionqueue.java:306)     @ org.hibernate.event.internal.abstractsaveeventlistener.addinsertaction(abstractsaveeventlistener.java:318)     @ org.hibernate.event.internal.abstractsaveeventlistener.performsaveorreplicate(abstractsaveeventlistener.java:275)     @ org.hibernate.event.internal.abstractsaveeventlistener.performsave(abstractsaveeventlistener.java:182)     @ org.hibernate.event.internal.abstractsaveeventlistener.savewithgeneratedid(abstractsaveeventlistener.java:113)     @ org.hibernate.event.internal.defaultpersisteventlistener.entityistransient(defaultpersisteventlistener.java:189)     @ org.hibernate.event.internal.defaultpersisteventlistener.onpersist(defaultpersisteventlistener.java:132)     @ org.hibernate.event.internal.defaultpersisteventlistener.onpersist(defaultpersisteventlistener.java:58)     @ org.hibernate.internal.sessionimpl.firepersist(sessionimpl.java:780)     @ org.hibernate.internal.sessionimpl.persist(sessionimpl.java:765)     @ mobi.puut.database.walletinfodao.create(walletinfodao.java:62)     @ mobi.puut.database.walletinfodao$$fastclassbyspringcglib$$624213d.invoke(<generated>)     @ org.springframework.cglib.proxy.methodproxy.invoke(methodproxy.java:204)     @ org.springframework.aop.framework.cglibaopproxy$cglibmethodinvocation.invokejoinpoint(cglibaopproxy.java:738)     @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:157)     @ org.springframework.dao.support.persistenceexceptiontranslationinterceptor.invoke(persistenceexceptiontranslationinterceptor.java:136)     @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179)     @ org.springframework.transaction.interceptor.transactioninterceptor$1.proceedwithinvocation(transactioninterceptor.java:99)     @ org.springframework.transaction.interceptor.transactionaspectsupport.invokewithintransaction(transactionaspectsupport.java:282)     @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:96)     @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179)     @ org.springframework.aop.framework.cglibaopproxy$dynamicadvisedinterceptor.intercept(cglibaopproxy.java:673)     @ mobi.puut.database.walletinfodao$$enhancerbyspringcglib$$a84b41b3.create(<generated>)     @ mobi.puut.services.walletservice.createwalletinfo(walletservice.java:273)     @ mobi.puut.services.walletservice.lambda$generateaddress$0(walletservice.java:97)     @ mobi.puut.controllers.walletmanager$1.lambda$onsetupcompleted$0(walletmanager.java:109)     @ java.lang.iterable.foreach(iterable.java:75)     @ java.util.collections$synchronizedcollection.foreach(collections.java:2062)     @ mobi.puut.controllers.walletmanager$1.onsetupcompleted(walletmanager.java:109)     @ org.bitcoinj.kits.walletappkit.startup(walletappkit.java:325)     @ com.google.common.util.concurrent.abstractidleservice$2$1.run(abstractidleservice.java:54)     @ com.google.common.util.concurrent.callables$3.run(callables.java:95) 

the project structure provided below,

enter image description here

the sample code directed error proviced below,

@transactional(rollbackfor = exception.class)     public walletinfo create(string name, string address) {          // create walletinfo entity provided name , address         walletinfo walletinfo = new walletinfo();         walletinfo.setaddress(address);         walletinfo.setname(name);          // persist created instance database         sessionfactory.getcurrentsession().persist(walletinfo);         return walletinfo;     }   protected walletinfo createwalletinfo(final string walletname, final string address) {         return walletinfodao.create(walletname, address);     }   public synchronized walletinfo generateaddress(final string walletname, string currencyname) {          // walletinfo entity database wallet , currency name         walletinfo walletinfo = walletinfodao.getwalletinfowithwalletnameandcurrency(walletname, currencyname);          // generate wallet, if wallet not         // generated         if (walletinfo == null) {              if (genwalletmap.get(walletname) == null) {                  logger.info("wallet name workign on {}", walletname);                  final walletmanager walletmanager = walletmanager.setupwallet(walletname);                  walletmanager.addwalletsetupcompletedlistener((wallet) -> {                      address address = wallet.currentreceiveaddress();          /*at mobi.puut.services.walletservice.lambda$generateaddress$0(walletservice.java:97) */                      walletinfo newwallet = createwalletinfo(walletname, address.tostring());                      walletmangersmap.put(newwallet.getid(), walletmanager);                     genwalletmap.remove(walletname);                 });                  genwalletmap.put(walletname, walletmanager);             }             return walletinfo;         }         return null;     }     private void setupwalletkit(final string walletid) {          file directory = getwalletdirectory(walletid);          // if seed not null, means restoring backup         bitcoin = new walletappkit(networkparameters, directory, wallet_file_name) {              @override             protected void onsetupcompleted() {                  // don't make user wait confirmations                 // they're sending own money anyway!!                 bitcoin.wallet().allowspendingunconfirmedtransactions();                 wallet wallet = bitcoin.wallet();                  model.setwallet(wallet);                  /* lambda$onsetupcompleted$0(walletmanager.java:109) */                 setupcompletedlisteners.foreach(listener -> listener.onsetupcompleted(wallet));             }         };          // code  } 

the walletinfo entity provided,

@entity @table(name = "wallet_info") public class walletinfo {      @id     @column(name = "id") //    @notnull     @generatedvalue(strategy = generationtype.identity)     private long id;      @notnull     @column(name = "name")     private string name;      @notnull     @column(name = "address")     private string address;      @notnull     @column(name = "currency")     private string currency;      public long getid() {         return id;     }      public walletinfo(@notnull string name, @notnull string address, @notnull string currency) {         this.name = name;         this.address = address;         this.currency = currency;     }      public walletinfo(@notnull string name, @notnull string address) {         this.name = name;         this.address = address;     }      public walletinfo() {     }      public void setid(long id) {         this.id = id;     }      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public string getaddress() {         return address;     }      public void setaddress(string address) {         this.address = address;     }       public string getcurrency() {         return currency;     }      public void setcurrency(string currency) {         this.currency = currency;     } } 

what issue here , how solve it?

the solution provided in comment correct, want write elaborately others, especially, using hibernate newly. walletinfo entity matches wallet_info table in mysql,

enter image description here

notice every entity provided non-null nn , hence, needs matched while persisting in database.

the walletinfo entity class after modification,

@entity @table(name = "wallet_info") public class walletinfo {      @id     @column(name = "id")     @generatedvalue(strategy = generationtype.identity)     private long id;      @notnull     @column(name = "name")     private string name;      @notnull     @column(name = "address")     private string address;      @notnull     @column(name = "currency")     private string currency;      // getters , setters       // constructors   public walletinfo(@notnull string name, @notnull string address, @notnull string currency) {         this.name = name;         this.address = address;         this.currency = currency;     }      public walletinfo(@notnull string name, @notnull string address) {         this.name = name;         this.address = address;     }      public walletinfo() {     } } 

in database layer, method using persist,

@transactional(rollbackfor = exception.class) public walletinfo create(string name, string currency, string address) {      // create walletinfo entity provided name , address     walletinfo walletinfo = new walletinfo();     walletinfo.setaddress(address);     walletinfo.setname(name);     walletinfo.setcurrency(currency);      // persist created instance database     sessionfactory.getcurrentsession().persist(walletinfo);     return walletinfo; } 

i added currecny column , hence, forget add walletinfo.setcurrency(currency) line made currency null earlier , creates error. still can make id null not element in constructor @notnull annotation , entity parameter definition not annotated @notnull

    @id     @column(name = "id")     @generatedvalue(strategy = generationtype.identity)     private long id; 

i hope people.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -