Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Last active November 25, 2015 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thjanssen/d85abc827de7252c01e6 to your computer and use it in GitHub Desktop.
Save thjanssen/d85abc827de7252c01e6 to your computer and use it in GitHub Desktop.
@Override
public void addItem(String product) {
log.info("Create item for product "+product);
Item i = new Item();
i.setProduct(product);
i.setOrder(o);
o.getItems().add(i);
this.em.persist(i);
}
14:30:17,539 INFO [ShoppingCart] (default task-1) Create item for product myFirstProduct
14:30:17,540 DEBUG [org.hibernate.SQL] (default task-1)
select
nextval ('hibernate_sequence')
14:30:17,548 INFO [ShoppingCart] (default task-1) Create item for product mySecondProduct
14:30:17,548 DEBUG [org.hibernate.SQL] (default task-1)
select
nextval ('hibernate_sequence')
14:30:17,454 INFO [ShoppingCart] (default task-1) Create order
14:30:17,464 DEBUG [org.hibernate.SQL] (default task-1)
select
nextval ('hibernate_sequence')
// 1. create the Order
cart.start();
// 2. add one Item to the Order
cart.addItem("myFirstProduct");
// 3. add additional Item to the Order
cart.addItem("mySecondProduct");
// 4. write Order with its Items to the database
cart.persist();
@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Column
private String product;
@ManyToOne
@JoinColumn(name = "fk_order")
private Order order;
...
}
14:30:17,748 DEBUG [org.hibernate.SQL] (default task-1)
select
item0_.id as id1_0_,
item0_.fk_order as fk_order3_0_,
item0_.product as product2_0_
from
Item item0_
14:30:17,756 DEBUG [org.hibernate.SQL] (default task-1)
select
order0_.id as id1_1_0_
from
my_order order0_
where
order0_.id=?
14:30:17,765 INFO [stdout] (default task-1) After joinTransaction: 2 items.
14:30:17,681 DEBUG [org.hibernate.SQL] (default task-1)
select
item0_.id as id1_0_,
item0_.fk_order as fk_order3_0_,
item0_.product as product2_0_
from
Item item0_
14:30:17,694 INFO [stdout] (default task-1) Before joinTransaction: 0 items.
14:30:17,694 INFO [ShoppingCart] (default task-1) Join transaction and write changes to the database
14:30:17,728 DEBUG [org.hibernate.SQL] (default task-1)
insert
into
my_order
(id)
values
(?)
14:30:17,739 DEBUG [org.hibernate.SQL] (default task-1)
insert
into
Item
(fk_order, product, id)
values
(?, ?, ?)
14:30:17,743 DEBUG [org.hibernate.SQL] (default task-1)
insert
into
Item
(fk_order, product, id)
values
(?, ?, ?)
@Entity(name="my_order")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@OneToMany(mappedBy = "order")
private List<Item> items = new ArrayList<Item>();
...
}
public void persist() {
log.info("Join transaction and write changes to the database");
this.em.joinTransaction();
}
@PersistenceContext(synchronization = SynchronizationType.UNSYNCHRONIZED, type = PersistenceContextType.EXTENDED)
EntityManager em;
@Stateful
public class ShoppingCart implements ShoppingCartRemote {
Logger log = Logger.getLogger(this.getClass().getSimpleName());
@PersistenceContext(synchronization = SynchronizationType.UNSYNCHRONIZED, type = PersistenceContextType.EXTENDED)
EntityManager em;
Order o;
@Override
public void start() {
log.info("Create order");
this.o = new Order();
this.em.persist(o);
}
@Override
public void addItem(String product) {
log.info("Create item for product "+product);
Item i = new Item();
i.setProduct(product);
i.setOrder(o);
o.getItems().add(i);
this.em.persist(i);
}
@Override
public void persist() {
log.info("Join transaction and write changes to the database");
this.em.joinTransaction();
}
}
public void start() {
log.info("Create order");
this.o = new Order();
this.em.persist(o);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment