Skip to content

Commit 4fec1ee

Browse files
committed
hibernate: Setup initial schema and db connection
1 parent 32ad708 commit 4fec1ee

File tree

6 files changed

+211
-2
lines changed

6 files changed

+211
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,48 @@
11
package com.cockroachlabs;
22

3+
import com.cockroachlabs.model.Customer;
4+
import com.cockroachlabs.model.Order;
5+
import com.cockroachlabs.model.Product;
6+
import org.hibernate.Session;
7+
import org.hibernate.SessionFactory;
8+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
9+
import org.hibernate.cfg.Configuration;
10+
import org.hibernate.service.ServiceRegistry;
11+
12+
import java.math.BigDecimal;
13+
314
public class Application {
415

516
public static void main(String[] args) {
17+
try (SessionFactory sf = buildSessionFactory()) {
18+
Session session = sf.getCurrentSession();
19+
20+
Customer c = new Customer();
21+
c.setName("joe");
22+
23+
Order o = new Order();
24+
o.setCustomer(c);
25+
o.setSubtotal(new BigDecimal(100));
26+
27+
session.beginTransaction();
28+
session.save(c);
29+
session.save(o);
30+
session.getTransaction().commit();
31+
}
32+
}
33+
34+
private static SessionFactory buildSessionFactory() {
35+
Configuration configuration = new Configuration();
36+
configuration.configure("hibernate.cfg.xml");
37+
configuration.addAnnotatedClass(Customer.class);
38+
configuration.addAnnotatedClass(Order.class);
39+
configuration.addAnnotatedClass(Product.class);
40+
41+
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
42+
.applySettings(configuration.getProperties())
43+
.build();
644

45+
return configuration.buildSessionFactory(serviceRegistry);
746
}
847

948
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.cockroachlabs.dialect;
2+
3+
import org.hibernate.dialect.PostgreSQL94Dialect;
4+
5+
public class CockroachDialect extends PostgreSQL94Dialect {
6+
7+
/**
8+
* Override needed to avoid referencing "pg_class".
9+
* <p/>
10+
* {@inheritDoc}
11+
*/
12+
@Override
13+
public String getQuerySequencesString() {
14+
return null;
15+
}
16+
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.cockroachlabs.model;
2+
3+
import javax.persistence.*;
4+
5+
@Entity
6+
@Table(name="Customers")
7+
public class Customer {
8+
9+
@Id
10+
@GeneratedValue(strategy=GenerationType.IDENTITY)
11+
@Column(name="ID", nullable=false, unique=true)
12+
private long id;
13+
14+
@Column(name="NAME")
15+
private String name;
16+
17+
public long getId() {
18+
return id;
19+
}
20+
21+
public void setId(long id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.cockroachlabs.model;
2+
3+
import javax.persistence.*;
4+
import java.math.BigDecimal;
5+
import java.util.Set;
6+
7+
@Entity
8+
@Table(name="Orders")
9+
public class Order {
10+
11+
@Id
12+
@GeneratedValue(strategy=GenerationType.IDENTITY)
13+
@Column(name="ID", nullable=false, unique=true)
14+
private long id;
15+
16+
@Column(name="SUBTOTAL", precision=18, scale=2)
17+
private BigDecimal subtotal;
18+
19+
@ManyToOne
20+
@JoinColumn(name="CUSTOMER_ID")
21+
private Customer customer;
22+
23+
@ManyToMany()
24+
@JoinTable(name="PRODUCT_ORDERS",
25+
joinColumns=@JoinColumn(name="ORDER_ID"),
26+
inverseJoinColumns=@JoinColumn(name="PRODUCT_ID"))
27+
private Set<Product> products;
28+
29+
public long getId() {
30+
return id;
31+
}
32+
33+
public void setId(long id) {
34+
this.id = id;
35+
}
36+
37+
public BigDecimal getSubtotal() {
38+
return subtotal;
39+
}
40+
41+
public void setSubtotal(BigDecimal subtotal) {
42+
this.subtotal = subtotal;
43+
}
44+
45+
public Customer getCustomer() {
46+
return customer;
47+
}
48+
49+
public void setCustomer(Customer customer) {
50+
this.customer = customer;
51+
}
52+
53+
public Set<Product> getProducts() {
54+
return products;
55+
}
56+
57+
public void setProducts(Set<Product> products) {
58+
this.products = products;
59+
}
60+
61+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.cockroachlabs.model;
2+
3+
import javax.persistence.*;
4+
import java.math.BigDecimal;
5+
import java.util.Set;
6+
7+
@Entity
8+
@Table(name="Products")
9+
public class Product {
10+
11+
@Id
12+
@GeneratedValue(strategy=GenerationType.IDENTITY)
13+
@Column(name="ID", nullable=false, unique=true)
14+
private long id;
15+
16+
@Column(name="NAME")
17+
private String name;
18+
19+
@Column(name="PRODUCT", precision=18, scale=2)
20+
private BigDecimal product;
21+
22+
@ManyToMany(cascade=CascadeType.ALL, mappedBy="products")
23+
private Set<Order> orders;
24+
25+
public long getId() {
26+
return id;
27+
}
28+
29+
public void setId(long id) {
30+
this.id = id;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name = name;
39+
}
40+
41+
public BigDecimal getProduct() {
42+
return product;
43+
}
44+
45+
public void setProduct(BigDecimal product) {
46+
this.product = product;
47+
}
48+
49+
public Set<Order> getOrders() {
50+
return orders;
51+
}
52+
53+
public void setOrders(Set<Order> orders) {
54+
this.orders = orders;
55+
}
56+
57+
}

java/hibernate/src/main/resources/hibernate.cfg.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
<property name="connection.pool_size">1</property>
1818

1919
<!-- SQL dialect -->
20-
<property name="dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
20+
<property name="dialect">com.cockroachlabs.dialect.CockroachDialect</property>
21+
<!--<property name="dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>-->
2122

2223
<!-- Echo all executed SQL to stdout -->
2324
<property name="show_sql">true</property>
2425

2526
<!-- Drop and re-create the database schema on startup -->
26-
<property name="hbm2ddl.auto">validate</property>
27+
<!--<property name="hbm2ddl.auto">validate</property>-->
28+
<property name="hbm2ddl.auto">update</property>
2729

2830
<!-- org.hibernate.HibernateException: No CurrentSessionContext configured! -->
2931
<property name="hibernate.current_session_context_class">thread</property>

0 commit comments

Comments
 (0)