generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 26
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
src/main/java/io/tbd/tbdex/protocol/processors/CloseProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
src/main/java/io/tbd/tbdex/protocol/store/MessageConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); |
63 changes: 63 additions & 0 deletions
63
src/test/java/io/tbd/tbdex/protocol/processors/MessageStoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 😢
There was a problem hiding this comment.
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