Commit b13abb66 authored by sapn1s's avatar sapn1s
Browse files

Added database

parent 65374936
......@@ -27,7 +27,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
......@@ -38,6 +41,28 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.zsoltfabok</groupId>
<artifactId>sqlite-dialect</artifactId>
<version>1.0</version>
</dependency>
<!-- Dependencies for Hibernate and ORM -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
</dependencies>
<build>
......
......@@ -7,8 +7,8 @@ import java.sql.Date;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.models.Event;
import com.example.models.EventType;
import com.example.calendar.models.Event;
import com.example.calendar.models.EventType;
@SpringBootApplication
public class CalendarApplication {
......
package com.example.calendar.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import com.example.calendar.models.User;
import com.example.calendar.repo.UserRepo;
@Controller
public class DBController {
@Autowired
UserRepo userRepo;
@GetMapping(value="/ok")
public String test()
{
User u1 = new User("Janis", "parole123");
userRepo.save(u1);
return "ok";
}
}
package com.example.models;
package com.example.calendar.models;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collection;
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.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "CalendarTable")
public class Calendar {
//TODO A lot of functions should be made in the controller with the help of a database
//e.g. deleteEventByName(), getEventsByName() <-- not needed.
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID_c")
private int calendar_ID; //Auto generated with JPA?
@OneToOne
@JoinColumn(name="ID_u")
private User user;
@OneToMany(mappedBy = "calendar")
private Collection<Event> event;
//TODO all calendar arrayList should not be in Calendar class.
//TODO Do we need eventList? maybe link events&calendar as oneToMany
private ArrayList<Event> eventList = new ArrayList<>();
......
package com.example.models;
package com.example.calendar.models;
import java.time.LocalDate;
import java.time.LocalTime;
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.Table;
@Entity
@Table(name = "EventTable")
public class Event {
@Column(name = "Name")
private String name;
@Column(name = "Description")
private String description;
@Column(name = "StartDate")
private LocalDate startDate;
@Column(name = "StartTime")
private LocalTime startTime;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID_e")
private int event_ID; //Auto-generated with JPA
@Column(name = "EventType")
private EventType eventType;
@Column(name = "IsAllDayEvent")
private boolean isAllDayEvent;
@ManyToOne
@JoinColumn(name = "ID_c")
private Calendar calendar;
......
package com.example.models;
package com.example.calendar.models;
public enum EventType {
......
package com.example.models;
package com.example.calendar.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "UserTable")
public class User {
//TODO spring secure auth
@Column(name = "Username")
private String username;
@Column(name = "Password")
private String password;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID_u")
private int user_ID; //Auto generated w/ jpa
@OneToOne(mappedBy = "user")
private Calendar calendar;
public User(String username, String password) {
setUsername(username);
......
package com.example.calendar.repo;
import org.springframework.data.repository.CrudRepository;
import com.example.calendar.models.User;
public interface UserRepo extends CrudRepository<User, Integer>{
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config/>
<!-- Tell Spring where to scan for components -->
<context:component-scan base-package="com.example.calendar" />
<!-- Specify the Hibernate properties setting file -->
<context:property-placeholder
location="classpath:META-INF/spring/hibernate.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses" value="com.example.calendar.models.User" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
\ No newline at end of file
jdbc.driverClassName=org.sqlite.JDBC
jdbc.url=jdbc:sqlite:calendar_db.sqlite
jdbc.username=root
jdbc.password=
hibernate.dialect=org.hibernate.dialect.SQLiteDialect
hibernate.show_sql=true
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h3>OK!</h3>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment