1 Reply Latest reply on Jan 9, 2007 7:15 AM by Reinhard Behrens

    How to create Rows in Database with EJB3.0 Entities - Proble

    Christian Froihofer Newbie

      Hi,

      I don't have much experience in writing Entity Java Beans, so if some questions are "dummy-questions", I apologize for that.

      Actual state: I have implemented Entity Java Beans with relationships between them. I can deploy them to JBOSS AS and everything is fine. Then, I want to implement a small client to fill the database with some values to check if my Beans are correct.

      Problem: I have three Beans, that have relationships.

      Entity Flight, Entity FlightSeat and Entity Address

      Flight has a OneToMany relationship with FlightSeat and two ManyToOne relationships with Address (for start- and destinationaddress of the flight)

      No, I want to create Values in the database with the Entities, but I don't know how I have to conside the relationships. How can I add a Flight row to the database? Do I have to specifiy the start- and destinationaddress, when saving the entity Flight with Entitymanager.persist()?? Maybe you know a good Tutorial where I can see, how it is possible to fill the tables?

      Here my Implementation:

      Flight.java:



      
      package at.ac.tuwien.inetappl.entity;
      
      import java.io.Serializable;
      
      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.OneToMany;
      import javax.persistence.Table;
      import javax.persistence.FetchType;
      
      import java.util.Date;
      import java.util.Collection;
      
      @Entity
      @Table (name="REL_FLIGHT")
      public class Flight implements Serializable{
      
       private long flightid;
       private String company;
       private Date duration;
       private Address startaddress;
       private Address destinationaddress;
       private Collection<FlightSeat> seats;
      
      
       public Flight() {}
      
      
       public Flight(String company, Date duration, Address startaddress, Address destinationaddress)
       {
       this.company=company;
       this.duration=duration;
       this.startaddress=startaddress;
       this.destinationaddress=destinationaddress;
      
       }
      
       @Id @GeneratedValue(strategy=GenerationType.AUTO)
       @Column(name="FLIGHT_ID")
       public long getId()
       {
       return this.flightid;
       }
      
       public void setId(long flightid)
       {
       this.flightid = flightid;
       }
      
       @Column(name="COMPANY")
       public String getCompany()
       {
       return this.company;
       }
      
       public void setCompany(String company)
       {
       this.company=company;
       }
      
       @Column(name="DURATION")
       public Date getDuration()
       {
       return this.duration;
       }
      
       public void setDuration(Date duration)
       {
       this.duration=duration;
       }
      
       @ManyToOne (optional=false)
       @JoinColumn(name = "START_ADDRESS_ID")
       public Address getStartAddress()
       {
       return this.startaddress;
       }
      
       public void setStartAddress(Address startaddress)
       {
       this.startaddress=startaddress;
       }
      
       @ManyToOne (optional=false)
       @JoinColumn(name = "DESTINATION_ADDRESS_ID")
       public Address getDestinationAddress()
       {
       return this.destinationaddress;
       }
      
       public void setDestinationAddress(Address destinationaddress)
       {
       this.destinationaddress=destinationaddress;
       }
      
       @OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="flight")
       //@JoinColumn(name= "SEAT_ID")
       public Collection<FlightSeat> getSeats()
       {
       return this.seats;
       }
      
       public void setSeats(Collection<FlightSeat> seats)
       {
       this.seats=seats;
       }
      }
      


      Address.java:

      
      package at.ac.tuwien.inetappl.entity;
      
      import java.io.Serializable;
      
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.Table;
      import javax.persistence.Column;
      
      @Entity
      @Table (name="REL_ADDRESS")
      public class Address implements Serializable{
      
       private String land;
       private String city;
       private String street;
       private int streetnumber;
       private int postalcode;
       private long addressid;
      
       public Address() {}
      
       public Address (String land, String city, String street,
       int streetnumber, int postalcode) {
      
       this.land=land;
       this.city=city;
       this.street=street;
       this.streetnumber=streetnumber;
       this.postalcode=postalcode;
       }
      
       @Id @GeneratedValue(strategy=GenerationType.AUTO)
       @Column(name="ADDRESS_ID")
       public long getId()
       {
       return this.addressid;
       }
      
       public void setId(long addressid)
       {
       this.addressid = addressid;
       }
      
       @Column(name="LAND")
       public String getLand()
       {
       return this.land;
       }
      
       public void setLand(String land)
       {
       this.land=land;
       }
      
       @Column(name="CITY")
       public String getCity()
       {
       return this.city;
       }
      
       public void setCity(String city)
       {
       this.city=city;
       }
      
       @Column(name="STREET")
       public String getStreet()
       {
       return this.street;
       }
      
       public void setStreet(String street)
       {
       this.street=street;
       }
       @Column(name="STREETNUMBER")
       public int getStreetNumber()
       {
       return this.streetnumber;
       }
      
       public void setStreetNumber(int streetnumber)
       {
       this.streetnumber=streetnumber;
       }
       @Column(name="POSTALCODE")
       public int getPostalCode()
       {
       return this.postalcode;
       }
      
       public void setPostalCode(int postalcode)
       {
       this.postalcode=postalcode;
       }
      }
      

      FlightSeat.java
      package at.ac.tuwien.inetappl.entity;
      
      import java.io.Serializable;
      
      import javax.persistence.Column;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.Entity;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.Table;
      
      @Entity
      @Table (name="REL_FLIGHTSEAT")
      public class FlightSeat implements Serializable {
      
       private long seatid;
       private String category;
       private int price;
       private boolean available;
       private Flight flight;
      
       public FlightSeat() {}
      
       public FlightSeat(String category, int price, boolean available)
       {
       this.category=category;
       this.price=price;
       this.available=available;
       }
      
       @Id @GeneratedValue(strategy=GenerationType.AUTO)
       @Column(name="SEAT_ID")
       public long getId()
       {
       return this.seatid;
       }
      
       public void setId(long seatid)
       {
       this.seatid = seatid;
       }
      
       @Column(name="CATEGORY")
       public String getCategory()
       {
       return this.category;
       }
      
       public void setCategory(String category)
       {
       this.category=category;
       }
      
       @Column(name="PRICE")
       public int getPrice()
       {
       return this.price;
       }
      
       public void setPrice(int price)
       {
       this.price=price;
       }
      
       @Column(name="AVAILABLE")
       public boolean getAvailability()
       {
       return this.available;
       }
      
       public void setAvailability(boolean available)
       {
       this.available=available;
       }
      
       @ManyToOne()
       @JoinColumn(name="FLIGHT_ID")
       public Flight getFlight()
       {
       return this.flight;
       }
      
       public void setFlight(Flight flight)
       {
       this.flight=flight;
       }
      }
      
      


      FlightBean.java - implementing business methods

      package at.ac.tuwien.inetappl.bean;
      
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.ejb.Remote;
      
      import at.ac.tuwien.inetappl.entity.Flight;
      import at.ac.tuwien.inetappl.entity.Address;
      import at.ac.tuwien.inetappl.entity.FlightSeat;
      
      import java.util.Collection;
      import java.util.Date;
      import java.util.Iterator;
      import java.io.Serializable;
      import javax.persistence.Query;
      
      
      
      @Stateless
      @Remote(FlightInterface.class)
      public class FlightBean implements FlightInterface, java.io.Serializable {
      
       //private Collection<Flight> flight;
      
       @PersistenceContext
       protected EntityManager em;
      
       /* CRUD METHODS FOR FLIGHT */
      
       /* CREATE FLIGHT */
      
       public void createFlight(String company, Date duration, Address start, Address dest)
       {
       Flight flight = new Flight();
       flight.setCompany(company);
       flight.setDuration(duration);
       flight.setStartAddress(start);
       flight.setDestinationAddress(dest);
       em.persist(flight);
       }
      
       public void updateFlight(Flight flight)
       {
      
       em.persist(flight);
      
       }
      
       /* DELETE FLIGHT*/
      
       public void deleteFlight(long id)
       {
      
       Query q = em.createQuery("DELETE FROM FLIGHT f WHERE f.FLIGHT_ID = :id");
       q.setParameter ("id", new Long(id));
      
       }
      
       /* SEARCH METHODS FOR FLIGHT */
      
       public Collection<Flight> getFlights()
       {
       return em.createQuery("from FLIGHT f").getResultList();
       }
      
       public Flight getFlight(long id) {
      
       Flight flight = em.find(Flight.class, new Long(id));
       return flight;
       }
      
       public String getCompany(long id) {
      
       Flight flight = em.find(Flight.class, new Long(id));
       String company = flight.getCompany();
       return company;
       }
      }
      


      Thanks to all!! I hope, i can find help here!
      Greetings,
      redbaron