本文介绍了休眠多对一的映射,我应该插入这种方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有两个表, user domain 。用户可能有一个域,一个域可能是许多用户。所以我们会做多方向的单向:

  @Entity 
@Table(name =DOMAIN)
public class Domain实现Serializable
{
@Id
@Column(name =DOMAIN_ID)
private Integer domainId;

@Column(name =NAME)
private String domainName;

$ b @Entity
@Table(name =\USER\)
public class User实现Serializable
{
@Id
@GeneratedValue(generator =USER_SEQ)
@GenericGenerator(name =USER_SEQ,strategy =sequence,parameters = @Parameter(name =sequence,value = SEQ_USER_ID))
@Column(name =USER_ID,nullable = false)
private长userId;

@Column(name =FIRST_NAME)
private String firstName;
$ b @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =DOMAIN_ID)
@ForeignKey(name =DOMAIN_ID)
私人域名;
}

表是字典,不可更改的东西。虽然 User 是可编辑表格。



我正在创建一个基本表单,用户在其中选择新用户被创建。假设我的控制器收到了这些数据:

  Integer domainId = 1; 
字符串firstName =aaa;

所以我创建了新用户:

  User newUser = new User(); 
newUser.setFirstName(firstName);

现在出现我的问题,我应该这样做吗?

 域名= somthingThatWillFetchObjectFromDb.getDomain(domainId); 
newUser.setDomain(domain);
//保存用户

这将生成额外的select,以获取域。当然,我可以使用 Integer domainId 来代替POJO,但这不是ORM。所以再次提出这个问题,这是应该怎么做的?解决方案

是的,那就是你应该做的。如果您不想实际从数据库加载域信息,请使用奇怪的名为 Session.load()方法而不是 Session。 get()方法。如果域尚未加载到会话中, Session.load()将简单地向您返回域的单位化实体代理(就像如果您已将某个实体加载到



也就是说,如果domain不可更改,为什么要设置 @Cascade(CascadeType .ALL)在域字段上?这意味着每次合并或更新用户时,该域也将被合并或更新。更糟糕的是:如果你删除了一个用户,这个域名也将被删除(如果其他用户引用同一个域名,这当然会导致异常)。


Let's say we have two tables, user and domain. User may have one Domain, one Domain may be for many users. So we will do unidirectional many to one:

@Entity
@Table(name = "DOMAIN")
public class Domain implements Serializable
{
    @Id
    @Column(name = "DOMAIN_ID")
    private Integer domainId;

    @Column(name = "NAME")
    private String domainName;
}

@Entity
@Table(name = "\"USER\"")
public class User implements Serializable
{
    @Id
    @GeneratedValue(generator = "USER_SEQ")
    @GenericGenerator(name = "USER_SEQ", strategy = "sequence", parameters = @Parameter(name = "sequence", value = "SEQ_USER_ID"))
    @Column(name = "USER_ID", nullable = false)
    private Long userId;

    @Column(name = "FIRST_NAME")
    private String firstName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "DOMAIN_ID")
    @ForeignKey(name = "DOMAIN_ID")
    private Domain domain;
}

Domain table is something unchangable, dictionary. While User is editable table.

I'm creating a basic form where user selects domain in which new user will be created. Lets say that my controller received those data:

Integer domainId = 1;
String firstName = "aaa";

So I'm creating new user:

User newUser = new User();
newUser.setFirstName( firstName );

Now comes my question, should I do this way?

Domain domain = somthingThatWillFetchObjectFromDb.getDomain( domainId );
newUser.setDomain( domain );
//save user

THis will generate additional select, to fetch domain. Of course I can use Integer domainId instead of POJO, but that's not ORM. So once again the question, is this the way it should be done?

解决方案

Yes, that's what you should do. If you don't want to actually load the domain information from the database, use the strangely named Session.load() method instead of the Session.get() method. If the domain is not already loaded into the session, Session.load() will simply return you an unitialized entity proxy for the domain (just like if you had loaded some entity with a lazy association to the domain), without hitting the database.

That said, if domain is unchangeable, why do you set @Cascade(CascadeType.ALL) on the domain field? This means that every time you're merging or updating a user, the domain will also be merged or updated. And even worse: if you delete a user, the domain will also be deleted (which of course will lead to an exception if other users are referencing the same domain).

这篇关于休眠多对一的映射,我应该插入这种方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 02:12