java ee - org.hibernate.MappingException: Unknown entity: domain.ShoppingCart$Proxy$_$$_WeldClientProxy -
i'm getting weird error, i'm not able debug. i'll obliged if can go through details , suggest me friends.
i'm using jboss server 7.1.
web app working fine modules. when i'm done products in cart, , "checkout", cause exception.
i provided required information, please if need else let me know. i'd appreciate kind responses. lot viewing request.
libraries included in project ( except jboss own included libraries )
hibernateutil.java
public static sessionfactory getsessionfactory() { try{ configuration conf=new configuration(); conf.configure("/com/webshop/hb/config/mycfg-mysql.xml"); sessionfactory =conf.buildsessionfactory(); }catch(exception e){ e.printstacktrace(); } return sessionfactory; }
shoppingcart.java
package domain; import java.text.dateformat; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import java.util.hashmap; import java.util.map; import java.util.set; import javax.enterprise.context.sessionscoped; import javax.inject.named; import javax.persistence.column; import javax.persistence.elementcollection; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.manytoone; import java.io.serializable; @named @sessionscoped @entity public class shoppingcart implements serializable { private static long currentid =1; @id @generatedvalue(strategy = generationtype.auto) @column(name="id") private long id; @column(name="date") private date date; @manytoone @joincolumn(name="user") private user user; @elementcollection private map<product, integer> orders = new hashmap<product, integer>(); @column(name="status") private orderstatus status; // string public shoppingcart() { this(new date()); } public shoppingcart(date date) { super(); this.id = currentid++; // simple way of getting unique id. deferred dbms later. this.date = date; this.status = orderstatus.empty; } public shoppingcart(date date, user user) { this(date); this.user = user; } public shoppingcart(date date, user user, orderstatus status) { this(date, user); if (((status == orderstatus.completed) || (status == orderstatus.paid)) && (this.getuser() == null)) { throw new illegalstateexception("no user specified complete or paid orders"); } } public date getdate() { return date; } public void setdate(date date) { this.date = date; } public user getuser() { return user; } public void setuser(user user) { this.user = user; } public map<product, integer> getorders() { return orders; } public void setorders(map<product, integer> orders) { this.orders = orders; } public orderstatus getstatus() { return status; } public void setcompleted(orderstatus status) throws illegalstatetransitionexception { if ((status == orderstatus.completed) && (this.getuser() == null)) { throw new illegalstatetransitionexception(this.getstatus().tostring(), orderstatus.completed.tostring(), "customer empty"); } if ((status == orderstatus.paid) && (this.getuser() == null)) { throw new illegalstatetransitionexception(this.getstatus().tostring(), orderstatus.paid.tostring(), "customer empty"); } if ((status == orderstatus.paid) && (this.getstatus() != orderstatus.completed)) { throw new illegalstatetransitionexception(this.getstatus().tostring(), orderstatus.paid.tostring(), "can go paid completed orders"); } this.status = status; } public long getid() { return id; } public void addtocart(product product, int amountselected) { if (orders.containskey(product)) { int amount = orders.get(product); amount +=amountselected; orders.put(product, amount); } else { orders.put(product, amountselected); } } public void removefromcart(product product) { if (orders.containskey(product)) { int amount = orders.get(product); amount --; if (amount <= 0) { orders.remove(product); } else { orders.put(product, amount); } } } public int getnumberofitems() { set<product> uniqueproducts = orders.keyset(); int amount = 0; (product aproduct: uniqueproducts) { amount += orders.get(aproduct); } return amount; } @override public string tostring() { dateformat dateformat = new simpledateformat("dd-mm-yyyy hh:mm:ss"); calendar cal = calendar.getinstance(); return "order of " + this.user + ", date " + dateformat.format(cal.gettime()); } }
mycfg-mysql.xml
<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.driver</property> <!-- <property name="hibernate.bytecode.use_reflection_optimizer">false</property> --> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> --> <property name="connection.datasource">java:jboss/datasources/webshopstudent</property> <property name="hibernate.connection.pool_size">100</property> <property name="dialect">org.hibernate.dialect.mysqldialect</property> <property name="show_sql">true</property> <!-- automatic schema creation (begin) === --> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.cache.provider_class">org.hibernate.cache.hashtablecacheprovider</property> <property name="current_session_context_class">thread</property> <mapping class="domain.user"/> <mapping class="domain.product"/> <mapping class="domain.wax"/> <mapping class="domain.miscellaneous"/> <mapping class="domain.honey"/> <mapping class="domain.flower"/> <mapping class="domain.shoppingcart"/> </session-factory> </hibernate-configuration>
exception i'm getting when checkout cart
function causing issue
public string confirmorder() throws illegalstatetransitionexception { shoppingcart.setuser(userholder.getcurrentuser()); shoppingcart.setcompleted(orderstatus.completed); shoppingcart.setdate(new date()); // setting sessions session session = hibernateutil.getsessionfactory().opensession(); session.begintransaction(); session.save(shoppingcart); // line causing exception, checked debugging session.close(); updatestocks(); facescontext fc = facescontext.getcurrentinstance(); fc.getelcontext().getelresolver().setvalue(fc.getelcontext(), null, "shoppingcart", null); //printing invoice //writing file try { string content = "customer name: " + shoppingcart.getuser().firstname + " " + shoppingcart.getuser().lastname +"\r\n" + "address : " + shippingaddress + "\r\n" + "date received: " + shoppingcart.getdate() + "\r\n" + "order items :" + shoppingcart.getorders() + "\r\n" + "money received :" + productholder.subtotal(); // file file = new file("/users/vinod/documents/newfile.txt"); file file = new file("f:/" + shoppingcart.getid() + shoppingcart.getuser().firstname + "invoice.txt"); if (!file.exists()) { file.createnewfile(); } filewriter fw = new filewriter(file.getabsolutefile()); bufferedwriter bw = new bufferedwriter(fw); bw.write(content); bw.close(); system.out.println("done"); }catch (ioexception e) { e.printstacktrace(); } return "ordercomplete"; }
for me reason pretty obvious: shoppingcart
entity cdi managed bean.
i'm sure injecting shoppingcart
somewhere above method confirmorder
:
@inject shoppingcart shoppingcart;
am right? if yes, cause.
generally jpa entities shouldn't managed cdi beans. reasoning behind prevent beanmanager perform operations may cause jpa provider break (as caused exception).
so please try this:
package domain; @entity public class shoppingcart implements serializable { // ... }
then define class, holds above entity:
package com.some_package; @named @sessionscoped public class shoppingcartwrapper { shoppingcart store = new shoppingcart(); // entity }
this class can injected wherever want. , saving like:
public string confirmorder() throws illegalstatetransitionexception { // ... session session = hibernateutil.getsessionfactory().opensession(); session.begintransaction(); session.save(shoppingcartwrapper.getstore()); session.close(); // ... }
btw: personal tip: managing transactions - mean "by hand", without ineterceptors error-prone , doesn't make code clean.
regards
Comments
Post a Comment