summaryrefslogtreecommitdiffstats
path: root/src/jpa/JPATest.java
blob: 5a4749e57dc3b82ba718ebe52df28d09b6c94295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class JPATest
{
   /**
    * @param args
    */
   public static void main(String[] args)
   {
      EntityManagerFactory factory = Persistence
            .createEntityManagerFactory("catalog");
      EntityManager manager = factory.createEntityManager();
      EntityTransaction tx = manager.getTransaction();
      tx.begin();
      try
      {
//         Product product = manager.find(Product.class, "7");
//         Category newCategory = manager.find(Category.class, 4);
//         product.addCategory(newCategory);
//         manager.persist(product);
//         Collection<Category> categories = product.getCategoryCollection();
//         for (Category category : categories)
//         {
//            System.out.println(category.getName());
//         }
         Product product = new Product();
         product.setDescription("Noch ein Testprodukt");
         product.setName("Produkt");
         manager.persist(product);
//         User user = new User();
//         user.setUsername("bestertester2");
//         System.out.println(DigestUtils.md5("masterkey"));
//         user.setPassword(DigestUtils.md5("masterkey"));
//         manager.persist(user);
         
         tx.commit();
      }
      catch (Exception ex)
      {
         ex.printStackTrace(System.err);
         tx.rollback();
      }
      
//      CategoryManager cm = new CategoryManager();
//      Collection<Category> categories = cm.getCategories();
//      for (Category category : categories)
//      {
//         System.out.println(category.getName());
//         Set<Product> products = category.getProductCollection();
//         for (Product product : products)
//         {
//            System.out.println("\t" + product.getName());
//         }
//
//      }
   }
}