Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save orekyuu/e4d71c566253331e5e9e9e597f0a3aef to your computer and use it in GitHub Desktop.
Save orekyuu/e4d71c566253331e5e9e9e597f0a3aef to your computer and use it in GitHub Desktop.
package com.example.demo;
import org.seasar.doma.jdbc.Result;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
EmployeeDao dao = context.getBean(EmployeeDao.class);
Result<Employee> result = dao.insert(new Employee(ID.notAssigned(), "test"));
System.out.println(result.getEntity().getId());
}
}
package com.example.demo;
import org.seasar.doma.*;
@Table(name = "employee")
@Entity(immutable = true)
public class Employee {
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private final ID<Employee> id;
@Column(name = "name")
private final String name;
public Employee(ID<Employee> id, String name) {
this.id = id;
this.name = name;
}
public ID<Employee> getId() {
return id;
}
public String getName() {
return name;
}
}
package com.example.demo;
import org.seasar.doma.Dao;
import org.seasar.doma.Insert;
import org.seasar.doma.boot.ConfigAutowireable;
import org.seasar.doma.jdbc.Result;
@ConfigAutowireable
@Dao
public interface EmployeeDao {
@Insert
Result<Employee> insert(Employee employee);
}
package com.example.demo;
import org.seasar.doma.Domain;
import java.io.Serializable;
import java.util.Objects;
/**
* EntityのIDを表すクラスです
* @param <T> Entityの型
*/
@Domain(valueType = long.class, factoryMethod = "of")
public final class ID<T> implements Serializable {
private static final long serialVersionUID = 1L;
private final long value;
private static final ID<Object> NOT_ASSIGNED = new ID<>(-1);
private ID(long value) {
this.value = value;
}
public Long getValue() {
if (this == NOT_ASSIGNED) {
return null;
}
return value;
}
/**
* IDを作るstatic factoryです。
* 0より小さいidでは生成することは出来ません。
* @param value id
* @param <R> Entityの型
* @return 生成されたID
* @throws IllegalArgumentException 0より小さいidを指定した時
*/
public static <R> ID<R> of(final long value) {
if (value < 0) {
throw new IllegalArgumentException("Illegal id: " + value);
}
return new ID<>(value);
}
/**
* 未割り当てのIDを表すIDインスタンスを返します。
* @param <R> Entityの型
* @return 未割り当てを表すIDインスタンス
*/
@SuppressWarnings("unchecked")
public static <R> ID<R> notAssigned() {
return (ID<R>) NOT_ASSIGNED;
}
@Override
public boolean equals(Object o) {
if (this == NOT_ASSIGNED) return false; //NOT_ASSIGNEDは常に等しくない
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ID<?> id = (ID<?>) o;
return value == id.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ID{");
sb.append("value=").append(value);
sb.append('}');
return sb.toString();
}
}
spring.thymeleaf.cache=false
doma.dialect=sqlite
spring.datasource.url=jdbc:sqlite:database.sqlite
spring.datasource.driver-class-name=org.sqlite.JDBC
spring.messages.encoding=UTF-8
CREATE TABLE employee(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULl
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment