Aplikacja hibernate

0

Zdobyłem jakieś zadanie rekrutacyjne od znajomego na staż i podjąłem się wyzwania zrobienia go dla sprawdzenia siebie.
Chciałem dodać brakujące relacje między klasą Tourists oraz klasą Flights do pliku mapującego w ten sposób:

<set name="xxx" cascade="all">
      <key column="xxx" />
      <one-to-many class="xxxx" />
    </set>

Następnie funcję addNewData() w metodzie App, która powinna tworzyć nowy lot wraz z przykładowymi pasażerami i zapisywać to w bazie danych.
Wykorzystałem już wszystkie możliwe sposoby, ale nie doszedłem do tego właściwego. Proszę o pomoc/podpowiedź (poniżej mój kod).

App.java

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class App {

	Session session;

	public static void main(String[] args) {
		App app = new App();
		app.addNewData();
		app.printTourists();
		app.close();
	}

	public App() {
		session = HibernateUtil.getSessionFactory().openSession();
	}

	public void close() {
		session.close();
		HibernateUtil.shutdown();
	}

	private void addNewData() {
		Tourists newTourist = new Tourists();
		newTourist.setId_tourist(3);
		newTourist.setName("Wacław");
		newTourist.setSurname("Paciołek");
		newTourist.setSex("Male");
		newTourist.setDate_of_birth("2000-01-10");
		newTourist.setCountry("Hungary");	
		
		Flight newFlight = new Flight();
		newFlight.setId_flight(4);
		newFlight.setDeparture_date_and_time("2019-11-11 08:09:10");
		newFlight.setArrival_date_and_time("2019-11-11 09:50:00");
		newFlight.setNumber_of_seats(411);
		newFlight.setTicket_price(109.99);
		
		// add newTourists and newFlight
		
		Transaction transaction = session.beginTransaction();
		session.save(newFlight);
		transaction.commit();
	}
	
	private void printTourists() {
		Criteria crit = session.createCriteria(Tourists.class);
		List<Tourists> tourist = crit.list();

		System.out.println("### List of tourists");
		for (Tourists t : tourist) {
			System.out.println(t);
			for (Flight fligth : t.getFlight()) {
	//			System.out.println("   " + flight);
			}
		}
	}
}

Flight.java

import java.sql.Date;
import java.util.HashSet;
import java.util.Set;

@SuppressWarnings("serial")

public class Flight implements java.io.Serializable {

	private Integer id_flight;
	private String departure_date_and_time;
	private String arrival_date_and_time;
	private Integer number_of_seats;
	private Integer list_of_tourists;
	private Double ticket_price;
	
	public Integer getId_flight() {
		return id_flight;
	}
	
	public void setId_flight(Integer id_flight) {
		this.id_flight = id_flight;
	}
	
	public String getDeparture_date_and_time() {
		return departure_date_and_time;
	}
	
	public void setDeparture_date_and_time(String departure_date_and_time) {
		this.departure_date_and_time = departure_date_and_time;
	}
	
	public String getArrival_date_and_time() {
		return arrival_date_and_time;
	}

	public void setArrival_date_and_time(String arrival_date_and_time) {
		this.arrival_date_and_time = arrival_date_and_time;
	}
	
	public Integer getNumber_of_seats() {
		return number_of_seats;
	}
	
	public void setNumber_of_seats(Integer number_of_seats) {
		this.number_of_seats = number_of_seats;
	}
	
	public Integer getList_of_tourists() {
		return list_of_tourists;
	}
	
	public void setList_of_tourists(Integer list_of_tourists) {
		this.list_of_tourists = list_of_tourists;
	}
	
	public Double getTicket_price() {
		return ticket_price;
	}
	
	public void setTicket_price(Double ticket_price) {
		this.ticket_price = ticket_price;
	}
	
	public String toString() {
		return "Flight: " + getDeparture_date_and_time() + " " + getArrival_date_and_time();
	}
}

Tourists.java

import java.util.HashSet;
import java.util.Set;

@SuppressWarnings("serial")


public class Tourists implements java.io.Serializable {
	private Integer id_tourist;
	private String name;
	private String surname;
	private String sex;
	private String country;
	private String notes;
	private String date_of_birth;
	private Integer list_of_flights;
	private Set<Flight> flight;
	
	public Tourists() {
		flight = new HashSet<Flight>();
	}
	
	public void addTouristToFlight(Flight touristToFlight) {
		flight.add(touristToFlight);
	}	
	
	public Set<Flight> getFlight() {
		return flight;
	} 
	
	public void setFlight(Set<Flight> flight) {
		this.flight = flight;
	}
	
	public Integer getId_tourist() {
		return id_tourist;
	}
	
	public void setId_tourist(Integer id_tourist) {
		this.id_tourist = id_tourist;
	}

	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getSurname() {
		return surname;
	}
	
	public void setSurname(String surname) {
		this.surname = surname;
	}
	
	public String getSex() {
		return sex;
	}
	
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public String getCountry() {
		return country;
	}
	
	public void setCountry(String country) {
		this.country = country;
	}
	
	public String getNotes() {
		return notes;
	}
	
	public void setNotes(String notes) {
		this.notes = notes;
	}
	
	public String getDate_of_birth() {
		return date_of_birth;
	}
	
	public void setDate_of_birth(String date_of_birth) {
		this.date_of_birth = date_of_birth;
	}
	
	public Integer getList_of_flights() {
		return list_of_flights;
	}
	
	public void setList_of_flights(Integer list_of_flights) {
		this.list_of_flights = list_of_flights;
	}	

	public String toString() {
		return "Tourists: " + getName() + " " + getSurname();
	}
}

HibernateUtil.java

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

	private static SessionFactory sessionFactory;
	
	static{
		try{
			sessionFactory = new Configuration().configure().buildSessionFactory();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static void shutdown(){
		getSessionFactory().close();
	}
}

JDBCmain.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCMain {
	public static void main(String[] args) {
		executeSQL();
	}

	private static void executeSQL() {
		Connection conn = null;
		Statement stmt = null;
		try {
			Class.forName("org.sqlite.JDBC");
			conn = DriverManager.getConnection("jdbc:sqlite:tourist.db", "", "");
			stmt = conn.createStatement();
			String sql = "SELECT * FROM tourists";
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				System.out.println("Tourists: " + rs.getString("name") + " " + rs.getString("surname"));	
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

PLIKI XML:

Flights.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="Flight" table="flights">
		<id name="id_flight" column="id_fligth" type="integer">
			<generator class="native"></generator>
		</id>
		
		<property name="departure_date_and_time" column="departure_date_and_time" type="string"></property>
		<property name="arrival_date_and_time" column="arrival_date_and_time" type="string"></property>
		<property name="number_of_seats" column="number_of_seats" type="integer"></property>
		<property name="list_of_tourists" column="list_of_tourists" type="integer"></property>
		<property name="ticket_price" column="ticket_price" type="double"></property>
		

	</class>
</hibernate-mapping>

Tourists.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="Tourists" table="tourists">
		<id name="id_tourist" column="id_tourist" type="integer">
			<generator class="native"></generator>
		</id>
	
		
		<property name="name" column="name" type="string"></property>
		<property name="surname" column="surname" type="string"></property>
		<property name="sex" column="sex" type="string"></property>
		<property name="country" column="country" type="string"></property>
		<property name="notes" column="notes" type="string"></property>
		<property name="date_of_birth" column="date_of_birth" type="string"></property>
		<property name="list_of_flights" column="list_of_flights" type="integer"></property>

	</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<property name="show_sql">false</property>
		<property name="format_sql">true</property>
		<property name="dialect">SQLiteDialect</property>
		<property name="connection.driver_class">org.sqlite.JDBC</property>
		<property name="connection.url">jdbc:sqlite:tourist.db</property>
		<property name="connection.username"></property>
		<property name="connection.password"></property>
	
		<mapping resource="Tourists.hbm.xml"/>
		<mapping resource="Flights.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
2

hibernate.cfg.xml

http://pluspng.com/img-png/what-year-is-it-png-robin-williams-jumanji-what-year-is-it-via-9gag-com-500.png

Przecież to jest jakiś Hibernate 2.0, starszy od ciebie. Gdyby ktoś coś takiego przyniósł jako zadanie rekrutacyjne to byłby winny śmierci rekruterów, ze śmiechu. Anyway: co dokładnie nie działa?

0
Mr. Moon napisał(a):

Chciałbym to zrozumieć zanim przejdę do adnotacji i do nowszych rzeczy. Chyba najprościej będzie zrozumieć to, a później ogarniać bardziej skomplikowane rzeczy, a nie jestem jeszcze wyjadaczem

Bardziej skomplikowane rzeczy? Te ustawienia w xml są trudniejsze niż odpowiednie zmapowanie relacji używając adnotacji. Na tym poziomie musisz wiedzieć jak to wygląda od strony SQL'a (jeden do wielu itp), nic więcej.

1 użytkowników online, w tym zalogowanych: 0, gości: 1