Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup hibernate #2

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ dependencies {
implementation 'com.google.guava:guava:30.1.1-jre'
implementation 'com.nimbusds:nimbus-jose-jwt:9.21'
implementation 'decentralized-identity:did-common-java:1.0.0'
implementation 'org.hibernate:hibernate-core:4.3.11.Final'
implementation 'org.hibernate.common:hibernate-commons-annotations:4.0.1.Final'
implementation 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final'
implementation 'mysql:mysql-connector-java:5.1.46'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package io.tbd.tbdex.protocol.processors;

public class CloseProcessor {
import io.tbd.tbdex.protocol.core.Message;

public class CloseProcessor implements MessageProcessor {
@Override public Message process(Message message) {
return null;
}
}
46 changes: 46 additions & 0 deletions src/main/java/io/tbd/tbdex/protocol/store/DbMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.tbd.tbdex.protocol.store;

import io.tbd.tbdex.protocol.core.Message;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "messages")
public class DbMessage {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="id")
private long id;

@Column(name="thread_token")
private String threadToken;

@Column(name="message", length = 65535, columnDefinition = "longblob")
@Convert(converter = MessageConverter.class)
private Message message;

public long getId() {
return id;
}

public String getThreadToken() {
return threadToken;
}

public void setThreadToken(String threadToken) {
this.threadToken = threadToken;
}

public Message getMessage() {
return message;
}

public void setMessage(Message message) {
this.message = message;
}
}
36 changes: 36 additions & 0 deletions src/main/java/io/tbd/tbdex/protocol/store/MessageConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.tbd.tbdex.protocol.store;

import io.tbd.tbdex.protocol.core.JsonParser;
import io.tbd.tbdex.protocol.core.Message;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter(autoApply=true)
public class MessageConverter implements AttributeConverter<Message, String> {

@Override
public String convertToDatabaseColumn(Message message) {

String messageJson = null;
try {
messageJson = JsonParser.getParser().toJson(message);
} catch (Exception e) {
System.out.println(e);
}

return messageJson;
}

@Override
public Message convertToEntityAttribute(String messageJSON) {

Message message = null;
try {
message = JsonParser.getParser().fromJson(messageJSON, Message.class);
} catch (Exception e) {
System.out.println(e);
}

return message;
}
}
14 changes: 14 additions & 0 deletions src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/tbdex-protocol</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="io.tbd.tbdex.protocol.store.DbMessage" />
</session-factory>
</hibernate-configuration>
12 changes: 12 additions & 0 deletions src/main/resources/migrations/v0001__messages.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE DATABASE `tbdex-protocol`;

USE `tbdex-protocol`;

CREATE TABLE messages (
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
thread_token VARCHAR(20) NOT NULL,
message longtext NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
KEY idx_thread_token(thread_token)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.tbd.tbdex.protocol.processors;

import io.tbd.tbdex.protocol.core.Message;
import io.tbd.tbdex.protocol.messages.Ask;
import io.tbd.tbdex.protocol.store.DbMessage;
import java.math.BigDecimal;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class MessageStoreTest {
@Test
@DisplayName("create DbMessage")
void test() {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.getTransaction();
try {
// Delete all asks
tx.begin();
Query query = session.createQuery("from DbMessage");
List<DbMessage> asks = query.list();
System.out.println("Employees found: " + asks.size());
for(DbMessage ask: asks) {
session.delete(ask);
System.out.println("Deleted " + ask);
}
tx.commit();

query = session.createQuery("from DbMessage");
Assertions.assertSame(query.list().size(), 0);


Message message = new Message.Builder("mid", "thid", "pfi", "alice")
.build(new Ask("USD", BigDecimal.valueOf(100), "USDC"));

// Create new Ask
tx = session.getTransaction();
tx.begin();
DbMessage ask = new DbMessage();
ask.setThreadToken(message.threadID());
ask.setMessage(message);
session.saveOrUpdate(ask);
tx.commit();

query = session.createQuery("from DbMessage");
Assertions.assertSame(query.list().size(), 1);
} catch (RuntimeException e) {
tx.rollback();
throw e;

} finally {
session.close();
}
}
}
14 changes: 14 additions & 0 deletions src/test/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pains me to see xml in 2022, but I understand 😢

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know :'( I will admit setting up an orm in Go was sooooooooo much nicer

<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/tbdex-protocol</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="io.tbd.tbdex.protocol.store.DbMessage" />
</session-factory>
</hibernate-configuration>
12 changes: 12 additions & 0 deletions src/test/resources/migrations/v0001__messages.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE DATABASE `tbdex-protocol`;

USE `tbdex-protocol`;

CREATE TABLE messages (
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
thread_token VARCHAR(20) NOT NULL,
message longtext NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
KEY idx_thread_token(thread_token)
);