Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.polsl.zti.db1.dao;
- import java.util.Date;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.EntityTransaction;
- import javax.persistence.Persistence;
- import pl.polsl.zti.db1.ConfigConsts;
- import pl.polsl.zti.db1.domain.Client;
- import pl.polsl.zti.db1.domain.Order;
- import pl.polsl.zti.db1.util.DbSchemaDef;
- import pl.polsl.zti.db1.util.JdbcUtils;
- import org.junit.Before;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import static org.junit.Assert.*;
- public class ClientOrdersTest {
- @BeforeClass
- public static void oneTimeSetUp() {
- }
- @Before
- public void setUp() {
- JdbcUtils.restoreDbSchema(new DbSchemaDef());
- }
- @Test
- public void testAddClientOrder() {
- //fail("OdkomentowaÄ poniĹźszy kod w trzeciej czÄĹci Äwiczenia.");
- final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(ConfigConsts.PERSISTANCE_UNIT_NAME);
- final EntityManager entityManager = entityManagerFactory.createEntityManager();
- final EntityTransaction transaction = entityManager.getTransaction();
- transaction.begin();
- Client client = new Client();
- client.setName("olo");
- Order order = new Order();
- order.setDescription("aaaa");
- order.setDate(new Date());
- order.setNo("111");
- order.setClient(client);
- client.getOrders().add(order);
- entityManager.persist(client);
- Client c = entityManager.find(Client.class, 1);
- for(Order o:c.getOrders())
- System.out.println(o.getDescription());
- transaction.commit();
- entityManager.close();
- long countClients = (Long) JdbcUtils.executeScalar("SELECT COUNT(*) FROM CLIENTS WHERE NAME='olo'");
- long countOrders = (Long) JdbcUtils.executeScalar("SELECT COUNT(*) FROM ORDERS WHERE ORDER_NO='111'");
- assertEquals("Klient nie zostaĹ dodany", 1, countClients);
- assertEquals("ZamĂłwienie nie zostaĹo dodane", 1, countOrders);
- }
- }
- ---------4 - - 1---------_-------
- package pl.polsl.zti.db1.domain;
- import java.io.Serializable;
- import java.util.Date;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- import javax.persistence.Temporal;
- import javax.persistence.TemporalType;
- @Entity
- @Table(name="ORDERS")
- public class Order implements Serializable {
- private static final long serialVersionUID = 1L;
- private int id;
- private String no;
- private Client client;
- private Date date;
- private String description;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- @Column(name = "ORDER_NO")
- public String getNo() {
- return no;
- }
- public void setNo(String no) {
- this.no = no;
- }
- @ManyToOne (fetch = FetchType.LAZY)
- @JoinColumn(name = "CLIENT_ID", nullable=false)
- public Client getClient() {
- return client;
- }
- public void setClient(Client client) {
- this.client = client;
- }
- @Column(name = "ORDER_DATE")
- @Temporal(TemporalType.DATE)
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- @Column(name = "ORDER_DESC")
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- }
- 11175/5%1=2+2/54/
- package pl.polsl.zti.db1.domain;
- import java.io.Serializable;
- import java.util.HashSet;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- @Entity
- @Table (name="clients")
- public class Client implements Serializable {
- private static final long serialVersionUID = 1L;
- private int id;
- private int no;
- private Set<Order> orders = new HashSet<Order>(0);
- private String name;
- private String ssn;
- private String address;
- @Id
- @GeneratedValue (strategy = GenerationType.IDENTITY)
- public int getId() {
- return this.id;
- }
- public void setId(int id) {
- this.id = id;
- }
- @Column (name="client_no")
- public int getNo() {
- return this.no;
- }
- public void setNo(int no) {
- this.no = no;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSsn() {
- return this.ssn;
- }
- public void setSsn(String ssn) {
- this.ssn = ssn;
- }
- public String getAddress() {
- return this.address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "client")
- public Set<Order> getOrders() {
- return this.orders;
- }
- public void setOrders(Set<Order> orders) {
- this.orders = orders;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement