Skip to content
Snippets Groups Projects
Commit c1cd3b20 authored by Leonard Haddad's avatar Leonard Haddad :rocket:
Browse files

Merge branch 'persistence' into 'master'

replaced ormlite with hibernate

See merge request reswp-2020/galaxytrucker!65
parents 50db613c 0d384eb9
No related branches found
No related tags found
No related merge requests found
Showing
with 209 additions and 61 deletions
......@@ -76,12 +76,32 @@ project(":core") {
// h2 database
runtimeOnly 'com.h2database:h2'
compile group: 'com.h2database', name: 'h2', version: '1.4.199'
// https://mvnrepository.com/artifact/com.h2database/h2
testCompile group: 'com.h2database', name: 'h2', version: '1.4.200'
// https://mvnrepository.com/artifact/org.hibernate/hibernate-core
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.17.Final'
// https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager
compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.4.17.Final'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.20'
// https://mvnrepository.com/artifact/org.mockito/mockito-core
testCompile group: 'org.mockito', name: 'mockito-core', version: '3.3.3'
// https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter
testCompile group: 'org.mockito', name: 'mockito-junit-jupiter', version: '3.3.3'
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.7.0-M1'
// ormlite
compile group: 'com.j256.ormlite', name: 'ormlite-jdbc', version: '4.48'
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'com.j256.ormlite:ormlite-core:4.48'
compile group: 'com.h2database', name: 'h2', version: '1.4.199'
}
}
No preview for this file type
File added
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="database" transaction-type="RESOURCE_LOCAL">
<class>com.galaxytrucker.galaxytruckerreloaded.Model.User</class>
<class>com.galaxytrucker.galaxytruckerreloaded.Model.Crew.Crew</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:./database;DB_CLOSE_ON_EXIT=FALSE;" />
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
......@@ -6,40 +6,39 @@ import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import lombok.*;
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@RequiredArgsConstructor(access = AccessLevel.PUBLIC)
@DatabaseTable(tableName = "crew")
@Entity
public class Crew implements Serializable {
/**
* ID
*/
@DatabaseField(id = true, columnName = "ID")
@Id
@NonNull
private int id;
/**
* Name
*/
@DatabaseField(columnName = "name")
@NonNull
private String name;
/**
* Health
*/
@DatabaseField(columnName = "health")
@NonNull
private int health;
/**
* Max health
*/
@DatabaseField(columnName = "maxhealth")
@NonNull
private int maxhealth;
......@@ -48,20 +47,17 @@ public class Crew implements Serializable {
* Das Array besteht aus
* [Weapon, Shield, Engine, Repair, Combat]
*/
@DatabaseField(columnName = "stats")
@NonNull
private int[] stats;
@ElementCollection
private List<Integer> stats;
/**
* The room this crew member is in
*/
@DatabaseField(foreign = true, columnName = "system")
private Room currentRoom;
/**
* The user who owns this crew member
*/
@DatabaseField(columnName = "associatedUser")
private String associatedUser;
}
package com.galaxytrucker.galaxytruckerreloaded.Model;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import lombok.*;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
@RequiredArgsConstructor(access = AccessLevel.PUBLIC)
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@Getter
@Setter
@DatabaseTable(tableName = "USER")
@Entity
public class User implements Serializable {
/**
* Username
*/
@DatabaseField(id = true, columnName = "USERNAME")
@Id
@NonNull
private String username;
/**
* The user's ship
*/
@DatabaseField(columnName = "userShip", foreign = true)
private Ship userShip;
/** Whether or not the user is logged in */
@NonNull
@DatabaseField(columnName = "loggedIn")
private boolean loggedIn = false;
}
package com.galaxytrucker.galaxytruckerreloaded.Server.Database;
import com.galaxytrucker.galaxytruckerreloaded.Model.User;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import lombok.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
@Getter
@NoArgsConstructor(access = AccessLevel.PUBLIC)
public class Database {
/**
* Database connection source
* Entity manager factory
*/
private ConnectionSource source;
private static final EntityManagerFactory entityManagerFactory;
/**
* Persistence unit name
*/
private static final String persistenceUnit = "database";
static {
entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit);
}
/**
* Load or create h2 database
* Get the current entityManager
*
* @return the entityManager
*/
public void setup() {
String url = "jdbc:h2:./database";
try {
source = new JdbcConnectionSource(url);
Dao<User, String> userDao = DaoManager.createDao(source, User.class);
try {
TableUtils.createTable(source, User.class);
} catch (Exception f) {
f.printStackTrace();
}
User u = new User("test");
userDao.create(u);
User user2 = userDao.queryForId("test");
System.out.println(user2.getUsername());
} catch (Exception e) {
e.printStackTrace();
}
public static EntityManager getEntityManager() {
return entityManagerFactory.createEntityManager();
}
/**
......
......@@ -3,15 +3,10 @@ package com.galaxytrucker.galaxytruckerreloaded.Server.Persistence;
import com.galaxytrucker.galaxytruckerreloaded.Model.Crew.Crew;
import com.galaxytrucker.galaxytruckerreloaded.Server.Exception.CrewNotFoundException;
import com.galaxytrucker.galaxytruckerreloaded.Server.Exception.DuplicateCrewException;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
public class CrewDAO extends ObjectDAO<Crew> {
import javax.persistence.EntityManager;
/**
* Crew dao
*/
private Dao<Crew, String> crewDao;
public class CrewDAO extends ObjectDAO<Crew> {
/**
* Add a new crew member to the database
......@@ -21,7 +16,14 @@ public class CrewDAO extends ObjectDAO<Crew> {
*/
@Override
public void persist(Crew c) throws DuplicateCrewException {
try {
entityManager.getTransaction().begin();
entityManager.persist(c);
entityManager.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
throw new DuplicateCrewException();
}
}
/**
......@@ -31,7 +33,14 @@ public class CrewDAO extends ObjectDAO<Crew> {
* @throws CrewNotFoundException if the crew cannot be found in the database
*/
public void update(Crew c) throws CrewNotFoundException {
try {
entityManager.getTransaction().begin();
entityManager.merge(c);
entityManager.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
throw new CrewNotFoundException();
}
}
/**
......@@ -42,14 +51,22 @@ public class CrewDAO extends ObjectDAO<Crew> {
*/
@Override
public void remove(Crew c) throws CrewNotFoundException {
try {
entityManager.getTransaction().begin();
entityManager.remove(c);
entityManager.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
throw new CrewNotFoundException();
}
}
/**
* Constructor
*
* @param source - database connection source
* @param entityManager - the EntityManager
*/
public CrewDAO(ConnectionSource source) {
public CrewDAO(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
package com.galaxytrucker.galaxytruckerreloaded.Server.Persistence;
import com.j256.ormlite.support.ConnectionSource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
/** Template for DAOs */
public abstract class ObjectDAO<T> {
/**
* Database connection
*/
private ConnectionSource source;
/** EntityManager */
@PersistenceContext(name = "database")
public EntityManager entityManager;
/**
* Save the object to the database
......
package com.galaxytrucker.galaxytruckerreloaded.Test.Server.Persistence;
import com.galaxytrucker.galaxytruckerreloaded.Model.Crew.Crew;
import com.galaxytrucker.galaxytruckerreloaded.Server.Database.Database;
import com.galaxytrucker.galaxytruckerreloaded.Server.Persistence.CrewDAO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import javax.persistence.EntityManager;
import java.util.UUID;
public class CrewDAOTest {
/**
* EntityManager
*/
private EntityManager entityManager = Database.getEntityManager();
/**
* CrewDAO
*/
private CrewDAO crewDAO = new CrewDAO(entityManager);
/**
* Test persisting a crew member to the database
*/
@Test
public void testPersist() {
Crew c = new Crew(UUID.randomUUID().hashCode(), "ahmad", 100, 200);
try {
crewDAO.persist(c);
} catch (Exception e) {
e.printStackTrace();
}
entityManager.getTransaction().begin();
Crew c2 = entityManager.find(Crew.class, c.getId());
entityManager.getTransaction().commit();
Assertions.assertEquals(c2.getName(), c.getName());
}
/**
* Test updating a crew member in the database
*/
@Test
public void testUpdate() {
Crew c = new Crew(UUID.randomUUID().hashCode(), "ahmad", 100, 200);
try {
crewDAO.persist(c);
} catch (Exception e) {
e.printStackTrace();
}
c.setName("da3esh");
try {
crewDAO.update(c);
} catch (Exception e) {
e.printStackTrace();
}
entityManager.getTransaction().begin();
Crew c2 = entityManager.find(Crew.class, c.getId());
entityManager.getTransaction().commit();
Assertions.assertEquals(c2.getName(), c.getName());
}
/**
* Test removing a crew member from the database
*/
@Test
public void testRemove() throws IllegalArgumentException {
Crew c = new Crew(UUID.randomUUID().hashCode(), "ahmad", 100, 200);
try {
crewDAO.persist(c);
crewDAO.remove(c);
} catch (Exception e) {
e.printStackTrace();
}
entityManager.getTransaction().begin();
try {
entityManager.find(Crew.class, c.getId());
} catch (Exception e) {
e.printStackTrace();
throw new IllegalArgumentException();
}
entityManager.getTransaction().commit();
}
}
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="database" transaction-type="RESOURCE_LOCAL">
<class>com.galaxytrucker.galaxytruckerreloaded.Model.User</class>
<class>com.galaxytrucker.galaxytruckerreloaded.Model.Crew.Crew</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:./database;DB_CLOSE_ON_EXIT=FALSE;" />
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
desktop/out/production/resources/1080p.png

4.31 MiB

desktop/out/production/resources/badlogic.jpg

66.9 KiB

desktop/out/production/resources/start_select2.png

6.56 KiB

File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment