Advertisement
Guest User

Entities + SessionBean

a guest
Nov 30th, 2011
105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package de.test.gwt;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Collection;
  5. import java.util.Vector;
  6.  
  7. import javax.persistence.Entity;
  8. import javax.persistence.Id;
  9. import javax.persistence.OneToMany;
  10.  
  11. @Entity
  12. public class Company implements Serializable{
  13.  
  14.     private static final long serialVersionUID = 1L;
  15.    
  16.     @Id
  17.     private String name;
  18.     @OneToMany
  19.     private Collection<Adress> adresses = new Vector<Adress>();
  20.    
  21.    
  22.     public Company(){
  23.        
  24.     }
  25.    
  26.     public Company(String name){
  27.         this.setName(name);
  28.     }
  29.  
  30.     public void addAdress(Adress adress) {
  31.         adresses.add(adress);
  32.     }
  33.  
  34.     public Collection<Adress> getAdress() {
  35.         return adresses;
  36.     }
  37.  
  38.     public void setName(String name) {
  39.         this.name = name;
  40.     }
  41.  
  42.     public String getName() {
  43.         return name;
  44.     }
  45.  
  46. }
  47.  
  48. _____________________________________________________________________________
  49.  
  50. package de.test.gwt;
  51.  
  52. import java.io.Serializable;
  53.  
  54. import javax.persistence.Entity;
  55. import javax.persistence.GeneratedValue;
  56. import javax.persistence.GenerationType;
  57. import javax.persistence.Id;
  58.  
  59. @Entity
  60. public class Adress implements Serializable{
  61.  
  62.  
  63.     private static final long serialVersionUID = 1L;
  64.    
  65.     @Id
  66.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  67.     private int id;
  68.     private String street;
  69.  
  70.     public Adress() {
  71.  
  72.     }
  73.  
  74.     public Adress(String street) {
  75.         this.street = street;
  76.     }
  77.  
  78.     public void setName(String street) {
  79.         this.street = street;
  80.     }
  81.  
  82.     public String getName() {
  83.         return street;
  84.     }
  85.  
  86.     public void setId(int id) {
  87.         this.id = id;
  88.     }
  89.  
  90.     public int getId() {
  91.         return id;
  92.     }
  93.  
  94. }
  95.  
  96. _____________________________________________________________
  97.  
  98. package de.test.gwt;
  99.  
  100. import java.util.List;
  101.  
  102. import javax.ejb.Stateless;
  103. import javax.persistence.EntityManager;
  104. import javax.persistence.PersistenceContext;
  105. import javax.persistence.Query;
  106.  
  107. @Stateless
  108. public class SampleBean implements SampleBeanLocal {
  109.  
  110.     private @PersistenceContext
  111.     EntityManager entityManager;
  112.  
  113.     public SampleBean() {
  114.     }
  115.  
  116.     public String createCompany(String text) throws Exception {
  117.         if (entityManager.find(Company.class, text) != null) {
  118.             throw new Exception("Name bereits vorhanden");
  119.         } else {
  120.            
  121.             Company company = new Company(text);
  122.            
  123.             Adress adress1 = new Adress("Google Street View Street 3");
  124.            
  125.             Adress adress2 = new Adress("Elch Street 23");
  126.  
  127.             company.addAdress(adress1);
  128.             company.addAdress(adress2);
  129.            
  130.             entityManager.persist(company);
  131.            
  132.             return company.getName();
  133.         }
  134.     }
  135. }
  136.  
  137.  
  138.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement