0

I'm working on an exercise where we're supposed to create a car-rental program in Java where all the data should be stored in a PostgreSQL database using JPA and EclipseLink.

I've managed to create a test-class which connects and stores/reads data to/from the database. Now I'm wondering how I should proceed to make this "big" car-rental program work together with the database...

We've got about 10 classes (Car.java, Customer.java, etc.), which I think based on an earlier example, should be connected to the main/client-classes (Customer_Client.java, Admin_Client.java, etc.) using a Controller-class(?). But I'm not quite sure how and why. If I understand it right, I think the database connecting code etc. is supposed to happen in the main/client-classes?

Could someone which is familiar with this kind of programming/modelling (ORM) point me in the right direction about how the Controller-class should work together with the client-classes?

Based on the earlier example, I guess the Controller-class should contain a getCars, getCustomers etc. method for all the classes I need to access in the main/client-classes?

I'm also wondering how I should add "custom"/class attributes (e.g. Adress.java) as an column in a table in the database? When I'm trying using the same method as with the String and Integers for e.g. the Adress attribute, I get this exception:

"Exception Description: The type [class no.hib.dat101.Adress] for the attribute [adress] on the entity class [class no.hib.dat101.Customer] is not a valid type for a serialized mapping. The attribute type must implement the Serializable interface."

I guess this has something to do with the database table-column only supports certain datatypes?

Ferdinand
  • 35
  • 6

3 Answers3

0

Basing on your exception, you should let no.hib.dat101.Adress implement java.util.Serializable so it is marked to serialize when saving a no.hib.dat101.Customer.

Smutje
  • 17,733
  • 4
  • 24
  • 41
0

I guess this has something to do with the database table-column only supports certain datatypes?

No, your issue is not related with database. How about adding implementsSerializable to your Adress class declaration?

Read about it more here.

Community
  • 1
  • 1
Everv0id
  • 1,862
  • 3
  • 25
  • 47
0

A controller class in ORM is usually a DAO. DAO is a pattern which defines how to create/read/update/delete objects from database. A general DAO can look like this:

    public interface Dao<E> implements Serializable{

        public E find(int id);

        public void insert(E entity);

        public void update(E entity);

        public void delete(int id);
    }

And its implementation (for example for Car entity) can look like this:

    public class CarDao implements Dao<Car>{

        private EntityManager em;

        public Car find(int id){
            return em.find(id, Car.class);
        }   

        public void insert(Car entity){
            em.persist(entity);
        } 

        public void update(Car entity){
            em.merge(entity);
        }

        public void delete(int id){
            em.delete(find(id));
        }

    }

For more info about DAO pattern please see Core J2EE Patterns - DAO (loooong but VERY good reading) or this link (shorter reading, but you will get a general idea about DAO faster :))

Entities update/insert is very easy, for example lets say that you want to set a new address for some customer.

    private CustomerDao customerDao;

    private Addressdao addressDao;

    private int customerId;

    public void updateCustomerWithAddress(){
        Address address = new Address();
        //init address variables

        addressDao.insert(address);  

        Customer customer = customerDao.find(customerId); 

        //I assume you have a bidirectional oneToOne mapping between address and customer
        address.setCustomer(customer);
        customer.setAddress(address);

        customerDao.update(customer);      
    }

In case of an exception you are getting, it says that your entities does not implement Serializable interface. So maybe by implementing this interface you will fix your issue, but we can really say much without actually seeing the code itself.

ioko
  • 991
  • 7
  • 4