`
darrenzhu
  • 浏览: 797537 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JoinColumn VS mappedBy

阅读更多
What is the difference between:

@Entity
public class Company {

    @OneToMany(cascade = CascadeType.ALL , fetch = FetchType.LAZY)
    @JoinColumn(name = "companyIdRef", referencedColumnName = "companyId")
    private List<Branch> branches;
    ...
}

and

@Entity
    public class Company {

        @OneToMany(cascade = CascadeType.ALL , fetch = FetchType.LAZY,
        mappedBy = "companyIdRef")
        private List<Branch> branches;
        ...
    }


Answer:
The annotation @JoinColumn indicates that this entity is the owner of the relationship (that is: the corresponding table has a column with a foreign key to the referenced table), whereas the attribute mappedBy indicates that the entity in this side is the inverse of the relationship, and the owner resides in the "other" entity.

In particular, for the code in the question the correct annotations would look like this:

@Entity
public class Company {
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "company")
    private List<Branch> branches;
}

@Entity
public class Branch {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "companyId")
    private Company company;
}


reference URL:http://stackoverflow.com/questions/11938253/jpa-joincolumn-vs-mappedby

对于mappedBy复习:
a) 只有OneToOne,OneToMany,ManyToMany上才有mappedBy属性,ManyToOne不存在该属性;
b) mappedBy标签一定是定义在the owned side(被拥有方的),他指向the owning side(拥有方);
c) mappedBy的含义,应该理解为,拥有方能够自动维护 跟被拥有方的关系;


   当然,如果从被拥有方,通过手工强行来维护拥有方的关系也是可以做到的。
d) mappedBy跟JoinColumn/JoinTable总是处于互斥的一方,可以理解为正是由于拥有方的关联被拥有方的字段存在,拥有方才拥有了被 拥有方。mappedBy这方定义的JoinColumn/JoinTable总是失效的,不会建立对应的字段或者表

分享到:
评论

相关推荐

    Hibernate注解 关联关系映射注解.docx

    此外,由于`mappedBy`的实体属性不再与数据库字段直接对应,所以不能使用`@JoinColumn`。 对于自关联,即一个实体类实例可以关联自身,我们同样可以使用`@ManyToOne`或`@OneToOne`注解,根据实际需求选择合适的关联...

    hibernate 关系注解

    双向一对一关系需要在两个实体类中都使用`@OneToOne`,一方使用`mappedBy`属性引用另一方的属性。例如: ```java // 在User类中 @OneToOne(mappedBy="user") private Profile profile; // 在Profile类中 @...

    hibernate对象三状态及OneToOne&OneToMany&ManyToMany

    mappedBy属性表示Profile实体是关系的拥有者,而JoinColumn注解用于指定外键列名。 三、一对多(OneToMany)关系映射 一对多关系意味着一个实体可以与多个其他实体相关联。例如,一个用户可以有多个订单: ```...

    hibernate各种常用关联关系的基本配置

    配置方式通常通过`@OneToOne`注解实现,可以设置`mappedBy`属性来指定在另一个实体中的反向关联字段。 ```java @Entity public class User { @OneToOne(mappedBy = "user") private Profile profile; } @Entity ...

    Hibernate的三种关系映射例子演示

    这里的`mappedBy`属性用于指定外键位于哪个实体,而`@JoinColumn`则用于指定外键所在的表和列名。 2. 一对多关系映射(One-to-Many) 在一对多关系中,一个实体可以对应多个其他实体的实例。例如,一个班级(Class...

    Hibernate 注解一对多,多对一

    1. 在使用`@OneToMany`时,如果没有指定`mappedBy`,那么在父类中需要使用`@JoinColumn`指定外键字段,并且这个字段通常会在数据库中创建一个索引。 2. 当使用`FetchType.LAZY`时,需要确保JPA实现支持懒加载,例如...

    Hibernate注释

    - **示例**:假设有一个班级类(`Class`)和一个学生类(`Student`),其中`Student`类中定义了`@ManyToOne`类型的`class`字段,则`Class`类中可以使用`@OneToMany(mappedBy = "class")`来定义一对多关系。...

    Hibernate关于注解的一对多,多对多,一对一

    你可以指定`mappedBy`属性来指定子类中的反向关联字段,这通常是多端的`@ManyToOne`注解的引用字段。 2. **@JoinColumn** 或 **@JoinTable**:对于一端,我们通常需要使用`@JoinColumn`注解来指定外键所在的列,...

    Hibernate_Annotation关联映射

    对于一对多的双向映射,如果要一对多这一端维护关联关系,你需要删除mappedBy元素并将多对一这端的@JoinColoumn的insertable和updatabel设置为false。这种方案不会得到什么明显的优化,而且还会增加一些附加的UPDATE...

    JPA一对一,一对多,多对多关系映射

    - 可以通过`mappedBy`属性指定被引用方,否则需在双方实体上定义关系。 - `@JoinColumn`注解用于指定外键列名和主键关联。 例如: ```java @Entity public class User { @Id private Long id; @OneToOne(mapped...

    Hibernate实体关联速查表

    这可以通过`@OneToOne`注解实现,可配置`mappedBy`属性来指定关联的反向字段。例如: ```java @Entity public class User { @OneToOne(mappedBy = "user") private Profile profile; } @Entity public class ...

    hibernate关联映射

    关联可以是单向或双向,单向关联只需在一方定义,双向关联则需要在双方都定义,并且需要设置`mappedBy`属性来指定主表。 ```java @Entity public class Person { @OneToOne(mappedBy = "person") private ...

    Hibernate表关系总结(一对一,多对一,多对多)

    - 双向:两个实体类互相引用,使用`mappedBy`属性指定双向关系的主导方。 ```java @Entity @Table(name = "_husband") public class Husband { // ... @OneToOne @JoinColumn(name = "wife-id") public Wife ...

    Hibernate 关联映射

    @OneToOne(mappedBy = "user") private Account account; } @Entity public class Account { @Id private Long id; @OneToOne @JoinColumn(name = "user_id") private User user; } ``` **2. 一对多关联...

    Hibernate 的多对一、一对多、多对多

    `@JoinTable`定义了中间表的名称以及两侧实体如何关联,而`mappedBy`属性则指定了关系的反向字段。 总结起来,Hibernate通过这三种关系映射机制,使得开发者能够轻松地处理复杂的数据关系,将Java对象模型和关系...

    hibernate 关联例子

    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL) private Profile profile; } @Entity public class Profile { @OneToOne @JoinColumn(name = "user_id") private User user; } ``` 这里,User和...

    hibernate 一对一 多对多的关系

    在上述代码中,`@OneToOne`注解用于声明一对一关系,`mappedBy`属性表示反向关联,而`@JoinColumn`则指定了关联字段的名称。 #### 外键(ForeignKey) ```java @Entity public class User { @Id @GeneratedValue...

    hibernate总结

    - **多对一(Many-to-One)**:在多方类中使用`@ManyToOne`,并在一方类中使用`@OneToMany(mappedBy="")`,其中mappedBy的值是多方类中对应的属性名。 - **多对多(Many-to-Many)**:这种关系比较复杂,通常需要...

Global site tag (gtag.js) - Google Analytics