Commit 6bbe1dca authored by doc. Ing. Jaroslav Porubän PhD.'s avatar doc. Ing. Jaroslav Porubän PhD.
Browse files

JPA

parent c5293a5b
......@@ -69,6 +69,24 @@
<artifactId>slf4j-jdk14</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.25.Final</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.11</version>
</dependency>
</dependencies>
<build>
......
package sk.tuke.dsl.testlang.main;
import sk.tuke.dsl.testlang.builder.MethodChainingBuilder;
import sk.tuke.dsl.testlang.model.Test;
import sk.tuke.dsl.testlang.semantics.TestPrinter;
import sk.tuke.dsl.testlang.serialization.JpaSerializer;
import java.io.IOException;
import static sk.tuke.dsl.testlang.builder.MethodChainingBuilder.test;
public class Main14JpaSerializer {
public static void main(String[] args) throws IOException, ClassNotFoundException {
/********************************************************************************/
test("Test z astronomie").
question("Kolko planet ma Slnecna sustava?").
correct_answer("8").
wrong_answer("6").
wrong_answer("7").
wrong_answer("9").
question("Zem je v poradi?").
correct_answer("3").
wrong_answer("1").
wrong_answer("2").
wrong_answer("4")
/********************************************************************************/
;
Test test = MethodChainingBuilder.getTest();
test.validate();
JpaSerializer serializer = new JpaSerializer();
//save
serializer.save(test);
//load
test = serializer.load(test.getIdTest());
TestPrinter testPrinter = new TestPrinter();
testPrinter.print(test);
}
}
package sk.tuke.dsl.testlang.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlAttribute;
import java.io.Serializable;
@Entity
public class Answer implements Serializable {
@Id
@GeneratedValue
@Column(name = "id_answer")
private long idAnswer;
private String text;
private boolean correct;
......@@ -16,6 +26,14 @@ public class Answer implements Serializable {
this.correct = correct;
}
public long getIdAnswer() {
return idAnswer;
}
public void setIdAnswer(long idAnswer) {
this.idAnswer = idAnswer;
}
public String getText() {
return text;
}
......
package sk.tuke.dsl.testlang.model;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlElement;
import java.io.Serializable;
import java.util.Arrays;
@Entity
public class Question implements Serializable {
@Id
@GeneratedValue
@Column(name = "id_question")
private long idQuestion;
private String text;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "id_question")
@OrderColumn(name = "answer_index")
private Answer[] answers;
public Question() {
......@@ -17,6 +27,14 @@ public class Question implements Serializable {
this.answers = answers;
}
public long getIdQuestion() {
return idQuestion;
}
public void setIdQuestion(long idQuestion) {
this.idQuestion = idQuestion;
}
public String getText() {
return text;
}
......
package sk.tuke.dsl.testlang.model;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
......@@ -15,9 +16,18 @@ import java.io.Serializable;
* String -> [a-zA-Z]+
*/
@XmlRootElement(namespace = "http://kpi.fei.tuke.sk/testlang")
@Entity
public class Test implements Serializable {
@Id
@GeneratedValue
@Column(name = "id_test")
private long idTest;
private String title;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "id_test")
@OrderColumn(name = "question_index")
private Question[] questions;
public Test() {
......@@ -28,6 +38,14 @@ public class Test implements Serializable {
this.questions = questions;
}
public long getIdTest() {
return idTest;
}
public void setIdTest(long idTest) {
this.idTest = idTest;
}
public String getTitle() {
return title;
}
......@@ -49,7 +67,7 @@ public class Test implements Serializable {
if (questions == null || questions.length == 0)
throw new TestLanguageException("Test musi mat aspon jednu otazku");
for(var question : questions) {
for (var question : questions) {
question.validate();
}
}
......
package sk.tuke.dsl.testlang.serialization;
import sk.tuke.dsl.testlang.model.Test;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
public class JpaSerializer {
public static final String PERSISTENCE_UNIT = "Test";
private EntityManager entityManager;
private EntityManager getEntityManager() {
if (entityManager == null) {
var entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
entityManager = entityManagerFactory.createEntityManager();
}
return entityManager;
}
public Test load(long id) {
EntityManager entityManager = getEntityManager();
return (Test) entityManager.find(Test.class, id);
}
public void save(Test test) {
EntityManager entityManager = getEntityManager();
var transaction = entityManager.getTransaction();
try {
transaction.begin();
entityManager.persist(test);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}
}
}
<persistence xmlns="http://java.sun.com/xml/ns/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">
<persistence-unit name="Test" transaction-type="RESOURCE_LOCAL">
<!-- Persistence provider -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!-- Entity classes -->
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/dsltest"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password" value="postgres"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL10Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<!-- <property name="hibernate.hbm2ddl.auto" value="update"/>-->
<property name="show_sql" value="true"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
</properties>
</persistence-unit>
</persistence>
......@@ -27,10 +27,10 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.12.0-rc1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.12.0-rc1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.12.0-rc1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.0-rc1" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.12.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.12.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.12.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.0" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.27" level="project" />
<orderEntry type="library" name="Maven: org.antlr:antlr4-runtime:4.7.1" level="project" />
<orderEntry type="library" name="Maven: javax.xml.bind:jaxb-api:2.4.0-b180830.0359" level="project" />
......@@ -44,5 +44,18 @@
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.9" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:2.0.0-alpha1" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-jdk14:2.0.0-alpha1" level="project" />
<orderEntry type="library" name="Maven: org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final" level="project" />
<orderEntry type="library" name="Maven: org.hibernate:hibernate-core:5.4.25.Final" level="project" />
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.4.1.Final" level="project" />
<orderEntry type="library" name="Maven: javax.persistence:javax.persistence-api:2.2" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.27.0-GA" level="project" />
<orderEntry type="library" name="Maven: net.bytebuddy:byte-buddy:1.10.17" level="project" />
<orderEntry type="library" name="Maven: antlr:antlr:2.7.7" level="project" />
<orderEntry type="library" name="Maven: org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:1.1.1.Final" level="project" />
<orderEntry type="library" name="Maven: org.jboss:jandex:2.1.3.Final" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.5.1" level="project" />
<orderEntry type="library" name="Maven: org.dom4j:dom4j:2.1.3" level="project" />
<orderEntry type="library" name="Maven: org.hibernate.common:hibernate-commons-annotations:5.1.2.Final" level="project" />
<orderEntry type="library" name="Maven: org.postgresql:postgresql:42.2.11" level="project" />
</component>
</module>
\ No newline at end of file
Supports Markdown
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