Last active
November 25, 2015 03:45
-
-
Save thjanssen/d85abc827de7252c01e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
(?, ?, ?) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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>(); | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void persist() { | |
log.info("Join transaction and write changes to the database"); | |
this.em.joinTransaction(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PersistenceContext(synchronization = SynchronizationType.UNSYNCHRONIZED, type = PersistenceContextType.EXTENDED) | |
EntityManager em; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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