title: Column cannot be null could not insert
body: <p>dear developers.</p>
  
  <p>I'm going to ask you a question which I think hard to solve for me just because I don't know the basic why to answer, 
           what I mean?? Let's see. The associations in JBoss documentations did not get the comprehensive answer: <strong>Why I should use JoinTable instead of Foreign Key</strong> and I do not completely understand how mappings works, 
                    What do I mean by this? I know what is association are ManyToMany or ManyToOne etc. and for what aims they are, <strong>but how they work's and collaborate with each other</strong> don't need an answer about bi-directional or unidirectional or joinTable or associations, I would like to have the link where I can find full info about my two questions:</p>
  
  <p>1) <strong>Why I should use JoinTable instead of Foreign Key</strong>????</p>
  
  <p>2) <strong>how entity work's and collaborate with each other</strong>(without explanations what is manyToMany etc. associations and bi or unidirectional associations are)???</p>
  
  <p>So, I have piece of code where I stuck because misunderstanding, I'm just trying to insert data I'm MYSQL database: Name_INSTITUTION and TYPE_NAME(Type of institution):*</p>
  
  <ul>
  <li><p>My entity class:</p>
  
  <pre><code>@Entity 
  @Table(name="INSTITUTION")
  
  public class Institution implements Serializable{
  
  private static final long serialVersionUID = -7636394097858726922L;
  
  
  private int Id;
  
      @Id
      @GeneratedValue(strategy=IDENTITY)
      @Column(name="ID")
      public int getId() {
          return Id;
      }
      public void setId(int id) {
          Id = id;
      }
  
  
  private int Version;
  
      @javax.persistence.Version
      @Column(name="VERSION")
      public int getVersion() {
          return Version;
      }
      public void setVersion(int version) {
          Version = version;
      }
  
  
  private String Name_Institution;
  
      @Column(name="NAME_INSTITUTION")
      public String getName_Institution() {
          return Name_Institution;
      }
      public void setName_Institution(String name_Institution) {
          Name_Institution = name_Institution;
      }
  
  
  private Type type_inInstitution;
  
      @ManyToOne  
      @Cascade(org.hibernate.annotations.CascadeType.ALL)
      @JoinTable(name="TYPE_INSTITUTION", 
                                      joinColumns=@JoinColumn(name="INSTITUTION_ID"),
                                      inverseJoinColumns=@JoinColumn(name="TYPE_ID"))
      public Type getType_inInstitution() {
          return type_inInstitution;
      }
      public void setType_inInstitution(Type type_inInstitution) {
          this.type_inInstitution = type_inInstitution;
      }
  </code></pre>
  
  <p>}</p></li>
  <li><p>My second entity class:</p>
  
  <pre><code>@Entity
  @Table(name="TYPE")
  
  public class Type implements Serializable {
  
  
  private static final long serialVersionUID = -4246217431412815552L;
  
  private String type;
  
      public Type(){}
  
      public Type(String type) {
      this.type = type;   
  
      }       
  
  private int Type_Id;
  
      @Id
      @GeneratedValue(strategy=IDENTITY)
      @Column(name="ID")
      public int getType_Id() {
          return Type_Id;
      }
      public void setType_Id(int type_Id) {
          Type_Id = type_Id;
      }
  
  
  private String Type_Name;
  
      @Column(name="TYPE_NAME")
      public String getType_Name() {
          return Type_Name;
      }
  
      public void setType_Name(String type_Name) {
          Type_Name = type_Name;
      }
  
  
  private int Version;
  
      @Version
      @Column(name="VERSION")
      public int getVersion() {
          return Version;
      }
      public void setVersion(int version) {
          Version = version;
      }
  
  
  
  private  Set&lt;Institution&gt; set_Institution_inType = new HashSet&lt;Institution&gt;();
  
      @OneToMany
      @JoinTable(name="TYPE_INSTITUTION",                                     joinColumns=@JoinColumn(name="TYPE_ID"),                                            inverseJoinColumns=@JoinColumn(name="INSTITUTION_ID"))
      public Set&lt;Institution&gt; getSet_Institution_inType() {
          return set_Institution_inType;
      }
      public void setSet_Institution_inType(Set&lt;Institution&gt; set_Institution_inType) {
          this.set_Institution_inType = set_Institution_inType;
      }
  
      public void addType(Institution institution) {
          institution.setType_inInstitution(this);
                 getSet_Institution_inType().add(institution);
  
      }
  </code></pre>
  
  <p>}</p>
  
  <p>The DAO Class:</p>
  
  <pre><code>@Repository("daoInsertDataInterface")
  @Transactional
  
  public class InsertDataService implements DaoInsertDataInterface{
  
  private org.apache.commons.logging.Log log=  LogFactory.getLog(InsertDataService.class);
  
  private SessionFactory sessionFactory;
  
      public SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      @Resource(name="sessionFactory")
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
  
  @Override
  public Institution insertData(Institution institution) {
      sessionFactory.getCurrentSession().saveOrUpdate(institution);
      log.info(institution.getId());
      return institution;
  
  }
  </code></pre>
  
  <p>}</p></li>
  <li><p>My metada config:</p>
  
  
  
  <pre><code>&lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
  &lt;property name="driverClassName"&gt;&lt;value&gt;${jdbc.driverClassName}&lt;/value&gt;&lt;/property&gt;
  &lt;property name="url"&gt;&lt;value&gt;${jdbc.connectionValues}&lt;/value&gt;&lt;/property&gt;
  &lt;property name="username"&gt;&lt;value&gt;${jdbc.userName}&lt;/value&gt;&lt;/property&gt;
  &lt;property name="password"&gt;&lt;value&gt;${jdbc.password}&lt;/value&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
  &lt;property name="sessionFactory" ref ="sessionFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;context:property-placeholder location="connection.properties"/&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
  &lt;context:component-scan base-package="edu.demidov.dom, edu.demidov.dao" /&gt;
  
  &lt;context:annotation-config /&gt;
  
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
  &lt;property name="dataSource" ref ="dataSource"/&gt;
  &lt;property name="packagesToScan" value="edu.demidov.dom"/&gt;
  &lt;property name="hibernateProperties"&gt;
  &lt;props&gt;
  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
  &lt;prop key="hibernate.max_fetch_depth"&gt;3&lt;/prop&gt;
  &lt;prop key="hibernate.jdbc.fetch_size"&gt;50&lt;/prop&gt;
  &lt;prop key="hibernate.jdbc.batch_size"&gt;10&lt;/prop&gt;
  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
  &lt;/props&gt;
  &lt;/property&gt;
  
  &lt;/bean&gt;
  </code></pre>
  
  <p></p></li>
  <li><p>And the error:</p></li>
  </ul>
  
  <blockquote>
    <p>INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader -
    Loading XML bean definitions from class path resource
    [app-context.xml] INFO :
    org.springframework.context.annotation.ClassPathBeanDefinitionScanner
    - JSR-330 'javax.inject.Named' annotation found and supported for component scanning INFO :
    org.springframework.context.support.GenericXmlApplicationContext -
    Refreshing
    org.springframework.context.support.GenericXmlApplicationContext@c93274:
    startup date [Thu May 09 11:38:23 EDT 2013]; root of context hierarchy
    INFO :
    org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
    - Loading properties file from class path resource [connection.properties] INFO :
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
    - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring INFO :
    org.springframework.beans.factory.support.DefaultListableBeanFactory -
    Pre-instantiating singletons in
    org.springframework.beans.factory.support.DefaultListableBeanFactory@1b9c2f0:
    defining beans
    [dataSource,transactionManager,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,daoInsertDataInterface,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,sessionFactory,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor];</p>
    
    <p>root of factory hierarchy Hibernate: insert into TYPE (TYPE_NAME,
    VERSION) values (?, ?) WARN : org.hibernate.util.JDBCExceptionReporter
    - <strong>SQL Error: 1048, SQLState: 23000 ERROR: org.hibernate.util.JDBCExceptionReporter - Column 'TYPE_NAME' cannot</strong>
    be null Exception in thread "main"
    org.hibernate.exception.ConstraintViolationException: could not
    insert: [edu.demidov.dom.Type]    at
    org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
      at
    org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
      at
    org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
      at
    org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2327)
      at
    org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2834)
      at
    org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)   at
    org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
      at
    org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
      at
    org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
      at
    org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
      at
    org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
      at
    org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
      at
    org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
      at
    org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
      at
    org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:252)
      at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)  at
    org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)     at
    org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)    at
    org.hibernate.engine.Cascade.cascade(Cascade.java:161)    at
    org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:450)
      at
    org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:282)
      at
    org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
      at
    org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
      at
    org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
      at
    org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
      at
    org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
      at
    org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
      at
    org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
      at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:673)
      at
    edu.demidov.dao.InsertDataService.insertData(InsertDataService.java:32)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)     at
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
      at
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
      at
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
      at
    org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
      at
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
      at
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
      at
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
      at $Proxy22.insertData(Unknown Source)  at
    edu.demidov.dao.AppTEst.main(AppTEst.java:22) 
    <strong>Caused by:
    com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
    Column 'TYPE_NAME' cannot be null</strong>   at
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
      at
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)     at
    com.mysql.jdbc.Util.getInstance(Util.java:386)    at
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)    at
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)    at
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)    at
    com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)     at
    com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)  at
    com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)   at
    com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
      at
    com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
      at
    com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
      at
    com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
      at
    org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
      at
    org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
      ... 44 more SS</p>
  </blockquote>
  
  <p>Thank you, guys.</p>