title: Hibernate: OneToMany relationship WITHOUTH cyclic dependency
body: <p>I have a <code>OneToMany</code> data model with two entities. One machine contains many characteristics. </p>
  
  <p><strong>Problem:</strong> I am trying to create entities without a cyclic dependency. </p>
  
  <p>But I get the issue: </p>
  
  <pre><code>  Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring-config.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1225)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:119)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:853)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
      at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
      at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
      at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
      ... 21 more
  Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: machine, for columns: [org.hibernate.mapping.Column(characteristics)]
      at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:336)
      at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310)
      at org.hibernate.mapping.Property.isValid(Property.java:241)
      at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:496)
      at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
      at org.hibernate.cfg.Configuration.validate(Configuration.java:1358)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
      ... 29 more
  </code></pre>
  
  <p><strong>My database:</strong> </p>
  
  <p><strong>machine:</strong> </p>
  
  <p><a href="https://i.stack.imgur.com/TrvG4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TrvG4.png" alt="enter image description here"></a></p>
  
  <p>create statement: </p>
  
  <pre><code>CREATE TABLE `machine` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(45) NOT NULL,
    `description` varchar(45) NOT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8
  </code></pre>
  
  <p>entity: </p>
  
  <pre><code>@Entity
  @Table(name = "machine")
  public class Machine {
      private int machine_id; 
  
      private String name;
  
      private String description; 
  
      private Set&lt;Characteristic&gt; characteristics;
  
      public Set&lt;Characteristic&gt; getCharacteristics() {
          return characteristics; 
      }
  
      public void setCharacteristics(Set&lt;Characteristic&gt; characteristics){
          this.characteristics = characteristics;
      }
  
      public Machine(){}
  
      public Machine(String name, String description){
          this.name = name;
          this.description = description; 
      }
  
      @Id 
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name="id")
      public int getId() {
          return machine_id;
      }
  
      public void setId(int machine_id) {
          this.machine_id = machine_id;
      }
  
      @Column(name="name")
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @Column(name="description")
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  }
  </code></pre>
  
  <p><strong>Characteristic</strong></p>
  
  <p><a href="https://i.stack.imgur.com/RwZQe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RwZQe.png" alt="enter image description here"></a></p>
  
  <p>create statement:</p>
  
  <pre><code>CREATE TABLE `characteristic` (
    `characteristic_id` int(11) NOT NULL AUTO_INCREMENT,
    `machine_id` int(11) NOT NULL DEFAULT '0',
    `name` varchar(45) DEFAULT NULL,
    `description` varchar(45) DEFAULT NULL,
    `type` int(11) NOT NULL,
    `value` int(11) DEFAULT NULL,
    PRIMARY KEY (`characteristic_id`),
  ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1
  </code></pre>
  
  <p>entity:</p>
  
  <pre><code>@Entity
  @Table(name = "characteristic")
  public class Characteristic {
      private int characteristic_id; 
  
      private String name; 
  
      private String description; 
  
      private int type; 
  
      private int value;
  
      public Characteristic() {}
  
      public Characteristic(String description, int type, int value) {
          this.description = description;
          this.type = type;
          this.value = value;
      } 
  
      public Characteristic(int characteristic_id, String description, int type, int value) {
          this.characteristic_id = characteristic_id;
          this.description = description;
          this.type = type;
          this.value = value;
      } 
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name="characteristic_id")
      public int getCharacteristic_Id() {
          return characteristic_id;
      }
  
      public void setCharacteristic_Id(int characteristic_id) {
          this.characteristic_id = characteristic_id;
      }
  
      @Column(name="name")
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @Column(name="description")
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      @Column(name="type")
      public int getType() {
          return type;
      }
  
      public void setType(int type) {
          this.type = type;
      }
  
      @Column(name="value")
      public int getValue() {
          return value;
      }
  
      public void setValue(int value) {
          this.value = value;
      }
  }
  </code></pre>
  
  <p>Spring-config.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:jpa="http://www.springframework.org/schema/data/jpa"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/data/jpa
      http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
      http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
  &lt;context:component-scan base-package="com.mego.smscloud" /&gt;
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/maschinedb?serverTimezone=UTC"/&gt;
          &lt;property name="username" value="root"/&gt;
          &lt;property name="password" value=""/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
          &lt;property name="showSql" value="true"/&gt;
          &lt;property name="generateDdl" value="true"/&gt;
          &lt;property name="database" value="MYSQL"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
          &lt;property name="jpaVendorAdapter" ref="jpaVendorAdapter"/&gt;
          &lt;property name="packagesToScan" value="com.mego.smscloud.entity"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/&gt;
  
      &lt;jpa:repositories base-package="com.mego.smscloud.repository" /&gt;
  
  &lt;/beans&gt;
  </code></pre>