Hibernate一对多关系的例子

时间:2014-02-22 18:28:26

标签: hibernate

使用HibernateUser对象与Role具有一对多的关系。我想用角色保存用户,请给我一些例子。

User.java:

@ Entity 
@ Table(name="user") 
public class User implements Serializable { 

  @ ManyToOne 
  @ JoinColumn(name="userTypeId") 
  private UserType userType; 
  //setters and getters

 } 

UserType.java:

@ Entity 
@ Table(name="userType") 
public class UserType {

@ Id 
@ Column(name="userTypeId") 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private int userTypeId; 

@ OneToMany(mappedBy="userType") 
private Set<User> users = new HashSet<User>(); 
//setters and getters

}

UserDaoImpl.java

@Repository("UserDao") 
public class UserDaoImpl extends AbstractDaoImpl<User, Integer> implements UserDao{     

@Autowired 
UserTypeDao userTypeDao; 
public User createUser(UserSignUpModel userSignupModel) throws BusinessException,SystemException { 
user = new User(); 
user.setUserType((UserType) getCurrentSession().get(UserType.class, ServerConstants.NEWLY_CREATED));
}
}

错误:

org.hibernate.MappingException: Unknown entity: com.gff.server.entity.UserType

gff-servlet.xml条目

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.gff.entity" />
    <!-- <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property> -->
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

0 个答案:

没有答案