-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Caused by: org.hibernate.PropertyValueException: not-null
PostPosted: Fri Jun 10, 2016 10:28 am 
Newbie

Joined: Thu Jun 09, 2016 5:12 am
Posts: 2
This is my code
Code:
package net.dontdrinkandroot.example.angularrestspringsecurity.entity;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;


@javax.persistence.Entity
@Table(name = "`Role`")
public class Role implements GrantedAuthority, Entity{
   
   private static final long serialVersionUID = 1L;
   
   
   @Id
   @GeneratedValue
   private Long id;
   
   @Column
   private String name;
   
   
   
   protected Role()
   {
      /* Reflection instantiation */
   }
   
   
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "user_id", nullable = false)
   private User user;
   public User getUser() {
      return this.user;
   }

   public void setUser(User user) {
      this.user = user;
   }
   
   public Role(String name){
      this.name = name;
   }
   
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getAuthority() {
        return this.name;
    }
   
}


Code:
package net.dontdrinkandroot.example.angularrestspringsecurity.entity;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;


@javax.persistence.Entity
@Table(name = "`User`")
public class User implements Entity, UserDetails
{

   
   
   
   @Id
   @GeneratedValue
   private Long id;

   @Column(unique = true, length = 16, nullable = false)
   private String name;

   @Column(length = 80, nullable = false)
   private String password;


   protected User()
   {
      /* Reflection instantiation */
   }

   public User(String name, String passwordHash)
   {
      this.name = name;
      this.password = passwordHash;
   }

   public Long getId()
   {
      return this.id;
   }

   public void setId(Long id)
   {
      this.id = id;
   }

   public String getName()
   {
      return this.name;
   }

   public void setName(String name)
   {
      this.name = name;
   }

   @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
   private Set<Role> roles = new HashSet<Role>(0);
   public Set<Role> getRoles() {
      return this.roles;
   }

   public void setRoles(Set<Role> role) {
      this.roles = role;
   }

   
   public void addRole(Role role)
   {
      this.roles.add(role);
   }



   public String getPassword()
   {
      return this.password;
   }

   public void setPassword(String password)
   {
      this.password = password;
   }

   public Collection<? extends GrantedAuthority> getAuthorities()
   {
      return this.getRoles();
   }

   public String getUsername()
   {
      return this.name;
   }

   public boolean isAccountNonExpired()
   {
      return true;
   }

   public boolean isAccountNonLocked()
   {
      return true;
   }

   public boolean isCredentialsNonExpired()
   {
      return true;
   }

   public boolean isEnabled()
   {
      return true;
   }
}


Code:

       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="username" value="${db.username}" />
      <property name="password" value="${db.password}" />
      <property name="driverClassName" value="${db.driver}" />
      <property name="url" value="${db.url}" />
   </bean>

   <bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="persistenceUnitName" value="examplePU" />
      <property name="jpaVendorAdapter">
         <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="showSql" value="true" />
         </bean>
      </property>
   </bean>

   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
   </bean>
   
   <bean id="newsEntryDao" class="net.dontdrinkandroot.example.angularrestspringsecurity.dao.newsentry.JpaNewsEntryDao">
   </bean>
   
   <bean id="userDao" class="net.dontdrinkandroot.example.angularrestspringsecurity.dao.user.JpaUserDao">
   </bean>
   
   <bean id="roleDao" class="net.dontdrinkandroot.example.angularrestspringsecurity.dao.role.JpaRoleDao">
   </bean>
   



when I try to run this code I get exception

Code:
public void initDataBase()
   {
      System.out.println();
      
      Role adminRole = new Role("ROLE_ADMIN");
      this.roleDao.save(adminRole);
      
      Role userRole = new Role("ROLE_USER");
      this.roleDao.save(userRole);
      
      User userUser = new User("user", this.passwordEncoder.encode("user"));
      userUser.addRole(userRole);
      this.userDao.save(userUser);
      


I am new with hibernate and need help

the relaation between user and role is one to many so each user has only one role and each role has many users


this is the exception

Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: net.dontdrinkandroot.example.angularrestspringsecurity.entity.Role.use


Top
 Profile  
 
 Post subject: Re: Caused by: org.hibernate.PropertyValueException: not-null
PostPosted: Fri Jun 10, 2016 2:01 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
It might be a cascading issue.

Try adding cascade = CascadeType.ALL to the @OneToMany association.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.