Advertisement
Guest User

EJB

a guest
Dec 1st, 2011
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package de.test.gwt;
  2. import javax.ejb.Remote;
  3.  
  4. @Remote
  5. public interface SampleBeanRemote {
  6.    
  7.     public String hello(String name) throws Exception;
  8.     public String[] getNames();
  9.     public String createCompany(String text) throws Exception;
  10.     public Company getCompany();
  11.  
  12. }
  13.  
  14.  
  15.  
  16.  
  17. package de.test.gwt;
  18.  
  19. import java.util.List;
  20.  
  21. import javax.ejb.Stateless;
  22. import javax.persistence.EntityManager;
  23. import javax.persistence.PersistenceContext;
  24. import javax.persistence.Query;
  25.  
  26. @Stateless
  27. public class SampleBean implements SampleBeanRemote {
  28.  
  29.     private @PersistenceContext
  30.     EntityManager entityManager;
  31.  
  32.     public SampleBean() {
  33.     }
  34.  
  35.     public String createCompany(String text) throws Exception {
  36.         if (entityManager.find(Company.class, text) != null) {
  37.             throw new Exception("Name bereits vorhanden");
  38.         } else {
  39.            
  40.             Company company = new Company(text);
  41.            
  42.             Adress adress1 = new Adress("Google Street View Street 3");
  43.            
  44.             Adress adress2 = new Adress("Elch Street 23");
  45.  
  46.             company.addAdress(adress1);
  47.             company.addAdress(adress2);
  48.            
  49.             entityManager.persist(company);
  50.            
  51.             return company.getName();
  52.         }
  53.     }
  54.  
  55.    
  56.     public Company getCompany() {
  57.         Query query = entityManager
  58.                 .createQuery("Select c from Company c");
  59.  
  60.         @SuppressWarnings("unchecked")
  61.         List<Company> companyList = (List<Company>) query
  62.                 .getResultList();
  63.  
  64.         return companyList.get(0);
  65.     }
  66. }
  67.  
  68.  
  69.  
  70.  
  71. package de.test.gwt;
  72.  
  73. import java.io.Serializable;
  74. import java.util.Collection;
  75. import java.util.Vector;
  76.  
  77. import javax.persistence.CascadeType;
  78. import javax.persistence.Entity;
  79. import javax.persistence.Id;
  80. import javax.persistence.OneToMany;
  81.  
  82. @Entity
  83. public class Company implements Serializable{
  84.  
  85.     private static final long serialVersionUID = 1L;
  86.    
  87.     @Id
  88.     private String name;
  89.     @OneToMany(cascade={CascadeType.PERSIST})
  90.     private Collection<Adress> adresses = new Vector<Adress>();
  91.    
  92.    
  93.     public Company(){
  94.        
  95.     }
  96.    
  97.     public Company(String name){
  98.         this.setName(name);
  99.     }
  100.  
  101.     public void addAdress(Adress adress) {
  102.         adresses.add(adress);
  103.     }
  104.  
  105.     public Collection<Adress> getAdress() {
  106.         return adresses;
  107.     }
  108.  
  109.     public void setName(String name) {
  110.         this.name = name;
  111.     }
  112.  
  113.     public String getName() {
  114.         return name;
  115.     }
  116.  
  117. }
  118.  
  119.  
  120.  
  121. package de.test.gwt;
  122.  
  123. import java.io.Serializable;
  124.  
  125. import javax.persistence.Entity;
  126. import javax.persistence.GeneratedValue;
  127. import javax.persistence.GenerationType;
  128. import javax.persistence.Id;
  129.  
  130. @Entity
  131. public class Adress implements Serializable{
  132.  
  133.  
  134.     private static final long serialVersionUID = 1L;
  135.    
  136.     @Id
  137.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  138.     private int id;
  139.     private String street;
  140.  
  141.     public Adress() {
  142.  
  143.     }
  144.  
  145.     public Adress(String street) {
  146.         this.street = street;
  147.     }
  148.  
  149.     public void setName(String street) {
  150.         this.street = street;
  151.     }
  152.  
  153.     public String getName() {
  154.         return street;
  155.     }
  156.  
  157.     public void setId(int id) {
  158.         this.id = id;
  159.     }
  160.  
  161.     public int getId() {
  162.         return id;
  163.     }
  164.  
  165. }
  166.  
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement