Hibernate One-To-Many Unidirectional Mapping

Group- User: one to many

In group class, we have a Set<User> users to store user;

1.Annotation

@Entity
@Table(name="x_Group")
public class Group {
    private int id;
    private String name;
    //for one-to-many illustration
    private Set<User> users=new HashSet<User>();

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

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

    @OneToMany
    @JoinColumn(name="groupId")
    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    public String getName() {
        return name;
    }

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

@Entity
@Table(name="x_user")
public class User {
   private int id;
   private String name;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

2.XML

<hibernate-mapping package="com.hibernate.model">
<class name="Group" table="T_Group">
   <id name="id"></id>
    <property name="name"></property>
    <set name="users">
      <key column="groupId"></key>
      <one-to-many class="com.hibernate.model.User"/>
    </set>
</class>

<hibernate-mapping package="com.hibernate.model">
<class name="User" table ="U_User">
   <id name="id"></id>
    <property name="name"></property>
</class>
时间: 2024-10-14 05:17:40

Hibernate One-To-Many Unidirectional Mapping的相关文章

Caused by:org.hibernate.DuplicateMappingException:Duplicate class/entity/ mapping

1.错误描述 java.lang.ExceptionInInitializerError Caused by:org.hibernate.InvalidMappingException:Could not parse mapping document from resource com/you/model/Monkey.hbm.xml Caused by:org.hibernate.DuplicateMappingException:Duplicate class/entity/ mapping

Atitit.解决org.hibernate.DuplicateMappingException: Duplicate class/entity mapping

Atitit.解决org.hibernate.DuplicateMappingException: Duplicate class/entity mapping 1. 排除流程::: @Deprecated public class HibernateSessionFactory { static { try { configuration.configure(propertyFile); sessionFactory = configuration.buildSessionFactory();

Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping com.you.flex.mode

1.错误描述 2015-09-21 00:02:09 [main] WARN org.springframework.context.support.GenericApplicationContext - Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanCreationException: Error cre

org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/domain/book.hbm.xml 联网跑一跑

org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/domain/book.hbm.xml at org.hibernate.cfg.Configuration.addResource(Configuration.java:586) at org.hibernate.cfg.Configuration.addClass(Configuration.java:633) a

org.hibernate.MappingException: Repeated column in mapping for entity: com.boot.entity.RepEntity column: rep_batch (should be mapped with insert=&quot;false&quot; update=&quot;false&quot;)

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.boot.entity.RepEntity column: rep_batch (should be mapped with insert="false" update="false") at org.hibernate.mapping.PersistentClass.checkColumnDup

Hibernate Many-to-Many Unidirectional mapping

In many to many, it creates three tables in total. Student,Teacher,Student_teacher In our example, all mappings are done in teacher class 1.Annotation Teacher: we create @ManyToMany @JoinTable(name="xx",@JoinColumns={@JoinColumn()},@inverseJoinC

hibernate查询异常:No Dialect mapping for JDBC type:-9(sql server)

今天用到hibernate查询数据库数据(sql server),出现异常:No Dialect mapping for JDBC type:-9 但是把对应的sql语句拷贝到数据库UI工具运行发现没有错误,所以考虑是hibernate查询的问题. 最终通过上网查资料发现hibernate对于nvarchar的查询问题,一般在中文系统中应该使用nvarchar作为字符串的对应类型,但是Hibernate中的默认实现SQLServerDialect使用了varchar. 解决方法: 1.自定义个一

hibernate 出现Could not parse mapping document from resource 报错

<!-- 映射文件的根节点 --> <hibernate-mapping > <!-- 对象关系映射的开始:class元素表示类和数据库中的表的映射关系. name属性指定持久化类(或者接口)的Java全限定名: table属性指定要映射的对应的数据库表名,如果省略,则以name作为表名 --> <class name="com.hibernate.customer.customer" table="customer">

org.hibernate.InvalidMappingException: Could not parse mapping document from无法创建sessionFactory

把 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" 改为 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 原文地址:https://www.cnblogs.com/zhaocundang/p/9922005.html