开发者

update hibernate JPA

IN hibernate JPA how can I update of a object? I have one Project entity having two attributes. projectid and projectname. Here projectid is primary key. Now in my upda开发者_开发百科te method i should be able to modify both the attributes projectid and projectname. The query shouldl look like the in the following way.

update PROJECT set ID='123',NAME='Intel' where ID='122'

I want to assign the project_id to the user_id.

project_table
-------------
project_id  project_name  
-------------------------  
LK987       LockSystem  
MK876       MockSystem      


user_project_table
---------------------  
user_id        project_id  
--------------------------  
12343       LK987  
12344       MK876  
12343       TK656  
12675       TK656  


Stop thinking in SQL, start thinking in EntityManager. Project p = entityManager.find(Project.class, 122); p.setId(123); p.setName("Intel"); entityManager.merge(p);

That said, it's almost always the wrong choice to change an entity's primary key.

Why would you change an entity's identity? You'd also need to update all the foreign keys in other tables that point to it. Seems like a pain, with no gain. You're probably better off making this a "business key" (plain property) and using a more permanent surrogate key.

If you really need to be able to change a project's ID number, you should change the primary key to an autoincremented number. The entity would look something like this:

@Entity
public class Project
{
    @Id @GeneratedValue
    private int id;
    private int projectId;
    private String projectName;

    // getters and setters
}


So you want to associate projects to users.

You can either do it where the project id (a string, like "LK987") is the primary key (which, by now I'm sure you understand you should not change) — or you can use a separate autoincrement int as the id. It's up to you; my personal preference is the autoincrement approach so you just don't have to worry about it later.

What you're trying to do is set up relationships between Project entities and User entities. Instead of storing foreign key fields, you should store entity fields (for one-to-one, one-to-many, or many-to one mappings) or collections of entities (for one-to-many, many-to one, or many-to-many mappings). If I understand what you need:

  • Each user can have multiple projects, and each project can have multiple users (so that mapping is many-to-many)
  • Users know about projects, and projects might as well know about users (so the mapping is bidirectional). I'll assume that the User is the owner of the relationship, and the Project is the inverse side.

That means you'll to use the @ManyToMany annotation, and specify a @JoinTable and which @JoinColumns to use.

User entity class

@Entity
public class User
{
    @Id @GeneratedValue
    int id;  // the PK for user entities

    @ManyToMany
    @JoinTable(
        name="user_project_table",
        joinColumns=@JoinColumn(name="user_id"),
        inverseJoinColumns=@JoinColumn(name="project_id"))
    Set<Project> projects;

    // snip...
}

Project entity class

@Entity
public class Project
{
    @Id @GeneratedValue
    int id;
    String projectId;
    String projectName;

    @ManyToMany(mappedBy="projects")
    Set<User> users;

    // snip...
}

Notice how we're using full-fledged entity classes, not just foreign keys.

Further reading from The Java EE 6 Tutorial:

  • Using Collections in Entity Fields and Properties
  • Multiplicity in Entity Relationships
  • Direction in Entity Relationships


If project id is the primary key and the identifier of an object, you should never change it. Change of primary key means that the represented object changes to something entirely different.

JPA assumes that the primary key is fixed. The read, updated and delete operations identifies the object in question by it's id. If you change the id of an object and execute an update, JPA either updates some completely different object (and this might result in an exception) or doesn't find an object to update at all.

To emphasize this, JPA spec forbids the change of primary key. Hibernate may also exclude the primary key from any generated update statement. See page 28 of JSR-317

The value of its primary key uniquely identifies an entity instance within a persistence context and to EntityManager operations as described in Chapter 3, “Entity Operations”. The application must not change the value of the primary key (this includes not changing the value of a mutable type that is primary key or an attribute of a composite primary key). The behavior is undefined if this occurs (the implementation may, but is not required to, throw an exception. Portable applications must not rely on any such specific behavior).

To accomplish the desired result, you should delete the old object and create a new one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜