I am using Hibernate to update the data in the MySQL database. I have two tables index and properties as described below.
The index_id in the first table is not auto-generated and I am assigning the value.
When I try to make index_id as part of the composite key in properties table, I am getting " Field 'index_id' doesn't have a default value". I am able insert the data manually in MySQL but my problem is that I am not able to do it via Hibernate code.
CREATE TABLE index (
index_id BINARY(16),
index_name VARCHAR(225) NOT NULL ,
config VARCHAR(255) NOT NULL,
PRIMARY KEY (index_id)
);
CREATE TABLE properties (
index_id BINARY(16),
property_key VARCHAR(225) NOT NULL,
property_value VARCHAR(225) NOT NULL,
PRIMARY KEY (index_id, property_key),
FOREIGN KEY (index_id) REFERENCES index(index_id)
);
Corresponding classes for the same with Hibernate annotations.
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;
import java.util.Set;
import java.util.UUID;
import com.sun.istack.NotNull;
@Entity
@Table(name = "index")
public class Index {
@Id
@Column(name = "index_id")
private UUID indexId;
@Column(name = "index_name")
private String indexName;
@Column(name = "config")
private String config;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "index_id")
private Set<Property> properties;
public Index() {}
}
The answers suggested in other posts are using auto-generated value which is not what I am looking for.
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "properties")
public class Property implements Serializable {
private static final long serialVersionUID = 1881413500711441951L;
@Id
@Column(name = "property_key")
private String propertyKey;
@Id
@Column(name = "property_value")
private String propertyValue;
public Property() {}
public Property(String propertyKey, String propertyValue) {
this.propertyKey = propertyKey;
this.propertyValue = propertyValue;
}
}
hibernate.cfg.xml
<?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.MySQLDialect</property>
<!-- JDBC Database connection settings -->
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/index?useSSL=false</property>
<property name="connection.username">###</property>
<property name="connection.password">###</property>
<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="connection.pool_size">1</property>
<!-- Select our SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Echo the SQL to stdout -->
<property name="show_sql">true</property>
<!-- Set the current session context -->
<property name="current_session_context_class">thread</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- dbcp connection pool configuration -->
<mapping class="com.learning.models.Index" />
<mapping class="com.learning.models.Property" />
</session-factory>
</hibernate-configuration>
HibernateUtil.java
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class HibernateUtil {
private static StandardServiceRegistry registry;
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
// Create registry
registry = new StandardServiceRegistryBuilder().configure().build();
// Create MetadataSources
MetadataSources sources = new MetadataSources(registry);
// Create Metadata
Metadata metadata = sources.getMetadataBuilder().build();
// Create SessionFactory
sessionFactory = metadata.getSessionFactoryBuilder().build();
} catch (Exception e) {
e.printStackTrace();
if (registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
}
return sessionFactory;
}
public static void shutdown() {
if (registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
}
I was able to resolve the issue. I had a one to many relation as given in the Index class
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "index_id")
private Set<Property> properties;
The issue was that I have to do a many to one mapping in the properties class with the index_id.
I changed the Properties class as below.
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "properties")
public class Property implements Serializable {
private static final long serialVersionUID = 1881413500711441951L;
@Id
@Column(name = "property_key")
private String propertyKey;
@Id
@Column(name = "property_value")
private String propertyValue;
@Id
@ManyToOne
@JoinColumn(name ="index_id")
private Index index;
public Property() {}
public Property(String propertyKey, String propertyValue, Index index) {
this.propertyKey = propertyKey;
this.propertyValue = propertyValue;
this.index = index;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.