Skip to content

Instantly share code, notes, and snippets.

@lausek
Created March 13, 2021 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lausek/650e7070c9d5ab944dcee4b3766b06c7 to your computer and use it in GitHub Desktop.
Save lausek/650e7070c9d5ab944dcee4b3766b06c7 to your computer and use it in GitHub Desktop.
CM Kryptographie: Hibernate ORM
package de.dhbw.model;
import com.sun.istack.NotNull;
import javax.persistence.*;
@Entity
@Table(name = "algorithms")
public class Algorithm {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@NotNull
private Integer id;
@Column(name = "name", unique = true)
@NotNull
private String name;
public Algorithm(String name) {
this.name = name;
}
private Algorithm() {
}
private void setId(Integer id) {
this.id = id;
}
private void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
package de.dhbw.model;
import com.sun.istack.NotNull;
import javax.persistence.*;
@Entity
@Table(name = "channel")
public class Channel {
@Id
@Column(name = "name", unique = true)
@NotNull
private String name;
@ManyToOne
@JoinColumn(name = "participant_01", nullable = false)
private Participant participant1;
@ManyToOne
@JoinColumn(name = "participant_02", nullable = false)
private Participant participant2;
private Channel() {
}
public Channel(String name, Participant participant1, Participant participant2) {
this.name = name;
this.participant1 = participant1;
this.participant2 = participant2;
}
public String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
public Participant getParticipant1() {
return participant1;
}
private void setParticipant1(Participant participant1) {
this.participant1 = participant1;
}
public Participant getParticipant2() {
return participant2;
}
private void setParticipant2(Participant participant2) {
this.participant2 = participant2;
}
}
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="hibernate.dialect.storage_engine">innodb</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="javax.persistence.schema-generation.database.action">
drop-and-create
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db?createDatabaseIfNotExist=true
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="show_sql">true</property>
<!-- List of class mapping -->
<mapping class="de.dhbw.model.Algorithm"/>
<mapping class="de.dhbw.model.Channel"/>
<mapping class="de.dhbw.model.Message"/>
<mapping class="de.dhbw.model.Participant"/>
<mapping class="de.dhbw.model.Postbox"/>
<mapping class="de.dhbw.model.Type"/>
</session-factory>
</hibernate-configuration>
package de.dhbw.model;
import com.sun.istack.NotNull;
import javax.persistence.*;
import java.time.Instant;
@Entity
@Table(name = "messages")
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@NotNull
private Integer id;
@ManyToOne
@JoinColumn(name = "participant_from_id", nullable = false)
private Participant participantFrom;
@ManyToOne
@JoinColumn(name = "participant_to_id", nullable = false)
private Participant participantTo;
@Column(name = "plain_message")
@NotNull
private String plainMessage;
@ManyToOne
@JoinColumn(name = "algorithm_id", nullable = false)
private Algorithm algorithm;
@Column(name = "encrypted_message")
@NotNull
private String encryptedMessage;
@Column(name = "keyfile")
@NotNull
private String keyfile;
@Column(name = "timestamp")
private Integer timestamp;
private Message() {}
public Message(Participant participantFrom,
Participant participantTo,
String plainMessage,
Algorithm algorithm,
String encryptedMessage,
String keyfile) {
this.participantFrom = participantFrom;
this.participantTo = participantTo;
this.plainMessage = plainMessage;
this.algorithm = algorithm;
this.encryptedMessage = encryptedMessage;
this.keyfile = keyfile;
this.timestamp = Math.toIntExact(Instant.now().getEpochSecond());
}
}
package de.dhbw.model;
import com.google.common.eventbus.Subscribe;
import com.sun.istack.NotNull;
import de.dhbw.App;
import de.dhbw.LogMessage;
import de.dhbw.factory.AlgorithmFactory;
import javax.persistence.*;
import java.io.File;
import java.lang.reflect.Method;
import java.util.Objects;
@Entity
@Table(name = "participants")
public class Participant {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@NotNull
private Integer id;
@Column(name = "name", unique = true)
@NotNull
private String name;
@ManyToOne
@JoinColumn(name = "type_id", nullable = false)
private Type type;
public Participant(String name, Type type) {
this.name = name;
this.type = type;
}
private Participant() {}
public Integer getId() {
return id;
}
private void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
public Type getType() {
return type;
}
private void setType(Type type) {
this.type = type;
}
public static Participant byName(App app, String name) {
var cq = app.getSession().createQuery("FROM Participant WHERE name = :name");
return (Participant) cq.setParameter("name", name).getSingleResult();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Participant that = (Participant) o;
return id.equals(that.id) &&
name.equals(that.name) &&
type.equals(that.type);
}
@Override
public int hashCode() {
return Objects.hash(id, name, type);
}
}
package de.dhbw.model;
import com.sun.istack.NotNull;
import javax.persistence.*;
@Entity
@Table(name = "postbox")
public class Postbox {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@NotNull
private Integer id;
@ManyToOne
@JoinColumn(name = "participant_to_id")
private Participant participantTo;
@ManyToOne
@JoinColumn(name = "participant_from_id", unique = true)
private Participant participantFrom;
@Column(name = "message")
@NotNull
private String message;
@Column(name = "timestamp")
private Integer timestamp;
}
package de.dhbw.model;
import com.sun.istack.NotNull;
import javax.persistence.*;
@Entity
@Table(name = "types")
public class Type {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@NotNull
private Integer id;
@Column(name = "name", unique = true)
@NotNull
private String name;
public Type(String name) {
this.name = name;
}
private Type() {
}
public String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
private void setId(Integer id) {
this.id = id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment