You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by avrono <av...@neuralm.com> on 2012/12/20 22:20:00 UTC
Help with JPA Entity Persist
Hi All,
Newbie trying to experiment with JPA2 (openjpa - TomEE). I would appreciate
any pointers on where I might be going wrong with the example below ....
I can read data from my Persistence-unit, but cannot write data (and no
error is generated) and I cannot see any generated SQL
The only log output I get is as follows:
=============================
25019 PRSX TRACE [http-bio-8080-exec-5] openjpa.jdbc.SQL - <t 25544092,
conn 20981618> executing prepstmnt 19451103 SELECT t0.bidEndDate,
t0.bidStartDate, t0.createdDate, t0.initialPrice, t0.name FROM item t0 WHERE
t0.id = ? [params=?]
25020 PRSX TRACE [http-bio-8080-exec-5] openjpa.jdbc.SQL - <t 25544092,
conn 20981618> [1 ms] spent
25023 PRSX TRACE [http-bio-8080-exec-5] openjpa.jdbc.SQL - <t 25544092,
conn 8418899> executing prepstmnt 30955499 SELECT t0.credit_rating,
t0.user_id FROM bidder t0 WHERE t0.id = ? [params=?]
25023 PRSX TRACE [http-bio-8080-exec-5] openjpa.jdbc.SQL - <t 25544092,
conn 8418899> [0 ms] spent
In add Bid : id = 0 bidStartDate = Thu Dec 20 21:12:24 GMT 2012 Initial
Price = 0.0 item_id = 0 item id from item is = 0 bidder_id = 1
Trying to persist !
20-Dec-2012 21:12:24 org.apache.myfaces.renderkit.html.HtmlLabelRenderer
encodeBegin
WARNING: Attribute 'for' of label component with id j_id_3:j_id_5 is not
defined
My persistence.xml:
===============
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="PRSX" transaction-type="JTA" >
<jta-data-source>jdbc/testDB</jta-data-source>
<class>com.prsx.dao.item.Item</class>
<properties>
<property name="openjpa.jdbc.DBDictionary" value="mysql"/>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)"/>
<property name="openjpa.Log" value="DefaultLevel=WARN,
Runtime=INFO, Tool=INFO, SQL=TRACE"/>
</properties>
</persistence-unit>
</persistence>
ManagedBean:
=============
@ManagedBean(name = "itemAdderBean")
@RequestScoped
//@TransactionManagement(TransactionManagementType.BEAN)
public class ItemAdderBean {
@Resource
private UserTransaction userTransaction;
@PersistenceUnit(unitName="PRSX")
private EntityManagerFactory entityManagerFactory;
/*
@PersistenceContext(unitName = "PRSX", type =
PersistenceContextType.TRANSACTION)
private EntityManagerFactory entityManagerFactory;
*/
@PersistenceContext
private EntityManager entityManager;
private int id =0;
private Date bidDate = null;
private double bidPrice = 0;
private int item_id = 0;
private int bidder_id = 0;
@SuppressWarnings("unused")
private List<Item> list;
private Item item;
public ItemAdderBean() {
System.out.println("Constructor of ItemAdderBean Called !");
item = new Item();
}
@SuppressWarnings("unchecked")
public List<Item> getList() throws Exception {
System.out.println("GetList of ItemAdderBean Called !");
Query query = entityManager.createQuery("Select i from Item i");
System.out.println("GetList of ItemAdderBean Called - returning !");
return query.getResultList();
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
/* Getter / Setter */
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public Date getBidDate() {
return this.bidDate;
}
public void setBidDate(Date bidDate) {
this.bidDate = bidDate;
}
public double getBidPrice() {
return this.bidPrice;
}
public void setBidPrice(double bidPrice) {
this.bidPrice = bidPrice;
}
public int getItem_id() {
return this.item_id;
}
public void setItem_id(int item_id) {
this.item_id = item_id;
}
public int getBidder_id() {
return this.bidder_id;
}
public void setBidder_id(int bidder_id) {
this.bidder_id = bidder_id;
}
public String addBid() {
System.out.println("Add Called !");
Bid bids = new Bid();
bids.setId(this.id);
bids.setBidDate(this.bidDate);
bids.setBidPrice(this.bidPrice);
Item theItem = entityManager.find(Item.class, this.item_id);
bids.setItem(theItem);
/* Just set bidder to 0 */
Bidder theBidder = entityManager.find(Bidder.class, 1);
bids.setBidder(theBidder);
String output = "In add Bid : id = " + this.id +
" bidStartDate = " + this.bidDate +
" Initial Price = " + this.bidPrice +
" item_id = " + this.item_id +
" item id from item is = " + this.item.getId() +
" bidder_id = " + this.bidder_id;
System.out.println(output);
//try {
//userTransaction = (UserTransaction)new
InitialContext().lookup("java:comp/UserTransaction");
//} catch (NamingException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
//}
//EntityTransaction entityTransaction = entityManager.getTransaction();
/* persist the bid */
//entityTransaction.begin();
try {
userTransaction.begin();
System.out.println("Trying to persist !");
entityManager.persist(bids);
userTransaction.commit();
} catch(Exception ex) {
ex.printStackTrace();
}
//entityTransaction.commit();
return "addBid";
}
public String reset (){
this.id=0;
this.bidDate = null;
this.bidPrice = 0;
this.item_id = 0;
this.bidder_id = 0;
return "reset";
}
@PostConstruct
public void init() {
this.entityManager = entityManagerFactory.createEntityManager();
/* Just make the dates NOW and NOW+14 days */
Calendar today = Calendar.getInstance();
this.bidDate = new java.util.Date(today.getTimeInMillis());
}
}
And Entity:
=========
@Entity
@Table(name="bids")
public class Bid implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Temporal( TemporalType.DATE)
private Date bidDate;
private double bidPrice;
//bi-directional many-to-one association to Bidder
@ManyToOne
private Bidder bidder;
//bi-directional many-to-one association to Item
@ManyToOne
private Item item;
public Bid() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public Date getBidDate() {
return this.bidDate;
}
public void setBidDate(Date bidDate) {
this.bidDate = bidDate;
}
public double getBidPrice() {
return this.bidPrice;
}
public void setBidPrice(double bidPrice) {
this.bidPrice = bidPrice;
}
public Bidder getBidder() {
return this.bidder;
}
public void setBidder(Bidder bidder) {
this.bidder = bidder;
}
public Item getItem() {
return this.item;
}
public void setItem(Item item) {
this.item = item;
}
}
--
View this message in context: http://openejb.979440.n4.nabble.com/Help-with-JPA-Entity-Persist-tp4659865.html
Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: Help with JPA Entity Persist
Posted by avrono <av...@neuralm.com>.
Yeah, that was it. Thanks !
--
View this message in context: http://openejb.979440.n4.nabble.com/Help-with-JPA-Entity-Persist-tp4659865p4659870.html
Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: Help with JPA Entity Persist
Posted by Romain Manni-Bucau <rm...@gmail.com>.
You probably miss a transaction. Use an ejb for instance (@stateless)
Le 20 déc. 2012 22:28, "avrono" <av...@neuralm.com> a écrit :
> Hi All,
>
> Newbie trying to experiment with JPA2 (openjpa - TomEE). I would appreciate
> any pointers on where I might be going wrong with the example below ....
>
> I can read data from my Persistence-unit, but cannot write data (and no
> error is generated) and I cannot see any generated SQL
>
> The only log output I get is as follows:
> =============================
>
> 25019 PRSX TRACE [http-bio-8080-exec-5] openjpa.jdbc.SQL - <t 25544092,
> conn 20981618> executing prepstmnt 19451103 SELECT t0.bidEndDate,
> t0.bidStartDate, t0.createdDate, t0.initialPrice, t0.name FROM item t0
> WHERE
> t0.id = ? [params=?]
> 25020 PRSX TRACE [http-bio-8080-exec-5] openjpa.jdbc.SQL - <t 25544092,
> conn 20981618> [1 ms] spent
> 25023 PRSX TRACE [http-bio-8080-exec-5] openjpa.jdbc.SQL - <t 25544092,
> conn 8418899> executing prepstmnt 30955499 SELECT t0.credit_rating,
> t0.user_id FROM bidder t0 WHERE t0.id = ? [params=?]
> 25023 PRSX TRACE [http-bio-8080-exec-5] openjpa.jdbc.SQL - <t 25544092,
> conn 8418899> [0 ms] spent
> In add Bid : id = 0 bidStartDate = Thu Dec 20 21:12:24 GMT 2012 Initial
> Price = 0.0 item_id = 0 item id from item is = 0 bidder_id = 1
> Trying to persist !
> 20-Dec-2012 21:12:24 org.apache.myfaces.renderkit.html.HtmlLabelRenderer
> encodeBegin
> WARNING: Attribute 'for' of label component with id j_id_3:j_id_5 is not
> defined
>
>
> My persistence.xml:
> ===============
>
> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
>
> <persistence-unit name="PRSX" transaction-type="JTA" >
> <jta-data-source>jdbc/testDB</jta-data-source>
> <class>com.prsx.dao.item.Item</class>
>
>
> <properties>
> <property name="openjpa.jdbc.DBDictionary" value="mysql"/>
> <property name="openjpa.jdbc.SynchronizeMappings"
> value="buildSchema(ForeignKeys=true)"/>
> <property name="openjpa.Log" value="DefaultLevel=WARN,
> Runtime=INFO, Tool=INFO, SQL=TRACE"/>
> </properties>
> </persistence-unit>
> </persistence>
>
>
> ManagedBean:
> =============
>
>
>
> @ManagedBean(name = "itemAdderBean")
> @RequestScoped
> //@TransactionManagement(TransactionManagementType.BEAN)
> public class ItemAdderBean {
>
> @Resource
> private UserTransaction userTransaction;
>
> @PersistenceUnit(unitName="PRSX")
> private EntityManagerFactory entityManagerFactory;
>
> /*
> @PersistenceContext(unitName = "PRSX", type =
> PersistenceContextType.TRANSACTION)
> private EntityManagerFactory entityManagerFactory;
> */
> @PersistenceContext
> private EntityManager entityManager;
>
> private int id =0;
> private Date bidDate = null;
> private double bidPrice = 0;
> private int item_id = 0;
> private int bidder_id = 0;
>
> @SuppressWarnings("unused")
> private List<Item> list;
> private Item item;
>
>
>
> public ItemAdderBean() {
> System.out.println("Constructor of ItemAdderBean Called
> !");
> item = new Item();
>
> }
>
> @SuppressWarnings("unchecked")
> public List<Item> getList() throws Exception {
> System.out.println("GetList of ItemAdderBean Called !");
> Query query = entityManager.createQuery("Select i from
> Item i");
> System.out.println("GetList of ItemAdderBean Called -
> returning !");
> return query.getResultList();
> }
>
> public Item getItem() {
> return item;
> }
>
> public void setItem(Item item) {
> this.item = item;
> }
>
> /* Getter / Setter */
> public int getId() {
> return this.id;
> }
>
> public void setId(int id) {
> this.id = id;
> }
>
> public Date getBidDate() {
> return this.bidDate;
> }
>
> public void setBidDate(Date bidDate) {
> this.bidDate = bidDate;
> }
>
> public double getBidPrice() {
> return this.bidPrice;
> }
>
> public void setBidPrice(double bidPrice) {
> this.bidPrice = bidPrice;
> }
>
> public int getItem_id() {
> return this.item_id;
> }
>
> public void setItem_id(int item_id) {
> this.item_id = item_id;
> }
>
> public int getBidder_id() {
> return this.bidder_id;
> }
>
> public void setBidder_id(int bidder_id) {
> this.bidder_id = bidder_id;
> }
>
>
>
> public String addBid() {
>
> System.out.println("Add Called !");
>
> Bid bids = new Bid();
> bids.setId(this.id);
> bids.setBidDate(this.bidDate);
> bids.setBidPrice(this.bidPrice);
>
> Item theItem = entityManager.find(Item.class,
> this.item_id);
> bids.setItem(theItem);
>
> /* Just set bidder to 0 */
> Bidder theBidder = entityManager.find(Bidder.class, 1);
> bids.setBidder(theBidder);
>
> String output = "In add Bid : id = " + this.id +
> " bidStartDate = " + this.bidDate +
> " Initial Price = " + this.bidPrice +
> " item_id = " + this.item_id +
> " item id from item is = " +
> this.item.getId() +
> " bidder_id = " + this.bidder_id;
>
> System.out.println(output);
>
> //try {
> //userTransaction = (UserTransaction)new
> InitialContext().lookup("java:comp/UserTransaction");
> //} catch (NamingException e) {
> // TODO Auto-generated catch block
> //e.printStackTrace();
> //}
>
> //EntityTransaction entityTransaction =
> entityManager.getTransaction();
>
> /* persist the bid */
> //entityTransaction.begin();
> try {
> userTransaction.begin();
> System.out.println("Trying to persist !");
> entityManager.persist(bids);
> userTransaction.commit();
> } catch(Exception ex) {
> ex.printStackTrace();
> }
> //entityTransaction.commit();
>
> return "addBid";
> }
>
> public String reset (){
>
> this.id=0;
> this.bidDate = null;
> this.bidPrice = 0;
> this.item_id = 0;
> this.bidder_id = 0;
> return "reset";
> }
>
> @PostConstruct
> public void init() {
>
> this.entityManager =
> entityManagerFactory.createEntityManager();
> /* Just make the dates NOW and NOW+14 days */
> Calendar today = Calendar.getInstance();
> this.bidDate = new java.util.Date(today.getTimeInMillis());
> }
> }
>
>
> And Entity:
> =========
> @Entity
> @Table(name="bids")
> public class Bid implements Serializable {
> private static final long serialVersionUID = 1L;
>
> @Id
> @Column(name="id")
> @GeneratedValue(strategy = GenerationType.IDENTITY)
> private int id;
>
> @Temporal( TemporalType.DATE)
> private Date bidDate;
>
> private double bidPrice;
>
> //bi-directional many-to-one association to Bidder
> @ManyToOne
> private Bidder bidder;
>
> //bi-directional many-to-one association to Item
> @ManyToOne
> private Item item;
>
> public Bid() {
> }
>
> public int getId() {
> return this.id;
> }
>
> public void setId(int id) {
> this.id = id;
> }
>
> public Date getBidDate() {
> return this.bidDate;
> }
>
> public void setBidDate(Date bidDate) {
> this.bidDate = bidDate;
> }
>
> public double getBidPrice() {
> return this.bidPrice;
> }
>
> public void setBidPrice(double bidPrice) {
> this.bidPrice = bidPrice;
> }
>
> public Bidder getBidder() {
> return this.bidder;
> }
>
> public void setBidder(Bidder bidder) {
> this.bidder = bidder;
> }
>
> public Item getItem() {
> return this.item;
> }
>
> public void setItem(Item item) {
> this.item = item;
> }
>
> }
>
>
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Help-with-JPA-Entity-Persist-tp4659865.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>