Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.test.gwt;
- import javax.ejb.Remote;
- @Remote
- public interface SampleBeanRemote {
- public String hello(String name) throws Exception;
- public String[] getNames();
- public String createCompany(String text) throws Exception;
- public Company getCompany();
- }
- package de.test.gwt;
- import java.util.List;
- import javax.ejb.Stateless;
- import javax.persistence.EntityManager;
- import javax.persistence.PersistenceContext;
- import javax.persistence.Query;
- @Stateless
- public class SampleBean implements SampleBeanRemote {
- private @PersistenceContext
- EntityManager entityManager;
- public SampleBean() {
- }
- public String createCompany(String text) throws Exception {
- if (entityManager.find(Company.class, text) != null) {
- throw new Exception("Name bereits vorhanden");
- } else {
- Company company = new Company(text);
- Adress adress1 = new Adress("Google Street View Street 3");
- Adress adress2 = new Adress("Elch Street 23");
- company.addAdress(adress1);
- company.addAdress(adress2);
- entityManager.persist(company);
- return company.getName();
- }
- }
- public Company getCompany() {
- Query query = entityManager
- .createQuery("Select c from Company c");
- @SuppressWarnings("unchecked")
- List<Company> companyList = (List<Company>) query
- .getResultList();
- return companyList.get(0);
- }
- }
- package de.test.gwt;
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.Vector;
- import javax.persistence.CascadeType;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- import javax.persistence.OneToMany;
- @Entity
- public class Company implements Serializable{
- private static final long serialVersionUID = 1L;
- @Id
- private String name;
- @OneToMany(cascade={CascadeType.PERSIST})
- private Collection<Adress> adresses = new Vector<Adress>();
- public Company(){
- }
- public Company(String name){
- this.setName(name);
- }
- public void addAdress(Adress adress) {
- adresses.add(adress);
- }
- public Collection<Adress> getAdress() {
- return adresses;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- }
- package de.test.gwt;
- import java.io.Serializable;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- @Entity
- public class Adress implements Serializable{
- private static final long serialVersionUID = 1L;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private int id;
- private String street;
- public Adress() {
- }
- public Adress(String street) {
- this.street = street;
- }
- public void setName(String street) {
- this.street = street;
- }
- public String getName() {
- return street;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getId() {
- return id;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement