Hi I´m new using GAE and JPA, and I´m having some problems trying to update an entity. I copy next a code example:
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
private String userName;
#ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
private Address address;
}
When I save on datastore a User instance it's store without problems.
After this, I retrive this instance from the datastore, then I set a new userName and try to update it.
public User updateUser(User user) {
EntityManager mgr = getEntityManager();
if (!containsUser(user)) {
throw new EntityNotFoundException("Object does not exist");
}
mgr.persist(user);
}
The update is performed, the new userName is stored in the dataStore, but as the Address field has FetchType.LAZY I'm losing the persisted value on the update.
How can I make an update of some fields without losing other values?
Try getting and saving the entity in the same method, otherwise you will lost the session and the manager will recognize the lazy attributes as null ones.
Hope it helps!
Related
I have a table with a DATETIME DEFAULT field.
CREATE TABLE People
(
Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
...
DtOccurrence DATETIME DEFAULT getDATE(),
);
Using scaffolding for generate Class and Entitity for Controllers + Views.
Default CRUD working fine, but if I try update a register, [DtOccurrence] get NULL in database.
How fix it? Thanks in advance
Create saving OK
Update only [Name] field send null [DtOccurrence] for database and my auto-generated class dont have this [DtOccurrence] field:
UPDATE:
CONTROLLER Create method
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name")] People people)
{
if (ModelState.IsValid)
{
_context.Add(people);
await _context.SaveChangesAsync();
return RedirectToAction("Edit", "Pessoas", new { people.Id });
}
return View(people);
}
CONTROLLER Edit method
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Name,")] People people)
{
if (id != people.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(people);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!PeopleExists(people.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(people);
}
Auto-generated class scaffolding
public partial class Pessoa
{
public Pessoa()
{
public int Id { get; set; }
public string Name { get; set; }
}
}
As mentioned in my comment, while your initial request to provide data to the view was given an entity from the DB Context, the object (Person) you get back in your Update method is not the same entity, and is not associated with your DbContext. It is a deserialized copy. Calling Update with it when it does not contain all fields will result in fields getting set to #null. Calling Update with a detached entity like this from a client is also an attack vector for unauthorized updates to your domain. (Debugging tools /plugins can intercept the call to the server and alter the entity data in any number of ways.)
public async Task<IActionResult> Edit(int id, [Bind("Id,Name,")] People people)
{
if (!ModelState.IsValid)
return View(people);
var dataPeople = await _context.People.SingleAsync(x => x.id == people.id);
dataPeople.name = people.name;
await _context.SaveChangesAsync(); // dataPeople is a tracked entity and will be saved, not people which is acting as a viewmodel.
return RedirectToAction(nameof(Index));
}
Using Update will generate an update statement where all fields on the entity are overwritten. You may decide to pass an incomplete entity to the view, or an incomplete entity back from the view, but EF has no notion of what data is missing because it wasn't provided/changed, vs. what was cleared out so it updates everything. Instead, you should load the entity from the DbContext based on the ID provided (which will error if the ID is not found) then set the properties you want to change on that tracked entity before calling SaveChanges. This ensures that the resulting SQL update statement contains only the columns you want changed.
As a general rule I recommend using view model classes for communicating models between server and client so it is clear what the data being passed around actually is. Passing entities between server and views is an anti-pattern which is prone to performance problems, serialization issues, and both intentional and accidental data corruption.
Additional validations should include making sure the changes are complete/legal, and potentially checking a row version # or last modified date between the passed model and the data loaded from the DB to ensure they match. When the user opened the page they may have gotten version #1 of the record. When they finally submit the form, if the DB returned version #2, it would indicate that someone else modified that row in that time. (Otherwise you are overwriting the changes)
I'm new with JPA and GAE. I'm having some problems trying to retrive some entities to the front end. I copy mi entity code:
#Entity
public class User implements Serializable{
private static final long serialVersionUID = -7949567692618870100L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
private String userName;
private String email;
#OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
private List<User> friends;
#OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Credential Credential;
//getters & setters
}
The Credential entity only contains userName and pass:
#Entity
public class Credential implements Serializable{
private static final long serialVersionUID = -3277842132830057420L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
private String userName;
private String pass;
//getters and setters
}
I generate UserEndpoint with GAE Eclipse Plugin and then I added a method to simulate a Login, this is the method:
#ApiMethod(name = "CredentialLogin")
public User CredentialLogin(#Named("pass") String pass,#Named("userName") String userName){
EntityManager mgr = getEntityManager();
Query query = mgr.createQuery("SELECT FROM User u WHERE u.email = :email");
query.setParameter("email", userName);
User user = null;
try {
user = (User) query.getSingleResult();
if (!user.getCredential().getPass().equals(pass))
//INVALID LOGIN
}
finally {
mgr.close();
}
return user;
}
Debugging the code I get the correct User from the datastore. Doing "user.getCredential()" the Credential fetchs to the User entity. As the "friends" field has LAZY fetching when I return the User entity I will appear NULL (This is what I want).
After the return I'm getting this error:
"com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: You have just attempted to access field "friends" yet this field was not detached when you detached the object. Either dont access this field, or detach it when detaching the object."
I don't want to put EAGER fetching to my "friends" attribute because the list could be really big.
I don't want to remove my "friends" getter and setter method, because without them I couldn't access to the field.
Somebody knows what should I do.
Thank you very much
You can change the enhancement process to call a listener instead of throwing an exception when an undetached field is accessed. See http://www.datanucleus.org/products/accessplatform_3_1/enhancer.html and "detachListener" option
I've done considerable research it doesn't work for me for some reason I don't even get into the situation where I get "zombie" instances(like existing in session but not in db anymore - I wish I had this situation) since remove() is not propagated into database. The child record continues to exist inside my db.
Consider two tables: User and Token(FK_User_Id) , relationship one-to-many respectively
Inside DAO final code which doesn't work:
public void deleteToken(Token t) {
Token b = em.merge(t);
em.remove(b); // does nothing
em.flush();
}
Inside controller:
Object obj;
Token token;
obj = tokenService.getByString(urlToken); // query returning detached object
User user;
if (obj != null) {
token = (Token) obj; // Detached, query is "fetch" overriding lazy init, so token contains its user parent
user = token.getUser(); // simply get the user to which this token belongs
if ((token.getExpiryDate().compareTo(new GregorianCalendar()
.getTime())) == 1) {
userService.activateUser(user); // user gets merged inside and its relevant property is changed and propagated to db successfully
tokenService.deleteToken(token); // this line calls DAO method described in snippet above - it fails to delete the token, but no error or exception - record simply stays in database
model.addAttribute("activationSuccess", "true");
}...
User entity:
public class User {
public static final String FIND_USER_BY_TOKEN = "findUserByToken";
public static final String FIND_USER_BY_USERNAME = "findUserByUsername";
public static final String FIND_USER_BY_EMAIL = "findUserByEmail";
#Id
#GeneratedValue
private Long id;
#OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval=true)
private List<Token> tokens = new ArrayList(); ...
Token entity:
#Entity
#Table(name = "token")
#NamedQueries({
#NamedQuery(name=Token.FIND_TOKEN_BY_STRING, query="Select t From Token T where t.tokenString=:string")
})
public class Token {
public static final String FIND_TOKEN_BY_STRING = "findTokenById";
#Id
#GeneratedValue
Long id;
#ManyToOne(optional=true)
private User user; ...
Now if I call something like:
User c = b.getUser();
em.remove(c);
Inside DAO snippet, it deletes both token and user which is not what I want. Only token must be deleted.
Basically what I am trying to do is to retrieve Token by string property and along with it the user parent which owns this token. Then I retrieve this user from token and change some property for the user(changes propagated into db). All successful so far. Then I want to delete the token as a final operation but in a way that user will not be deleted.
I am on my fifth hour on this please help... I managed to setId to null for the token(propagated into db), but it only gets me the point where token no longer has owner but still persists in database. To delete it I tried to merge the Token inside DAO with null which threw me exception. Then I tried to set Tokens list value to null inside User(which was retrieved from this token) and it also through me exception.
How I am supposed to delete child token entity which I retrieved with its parent but keep parent present in db?
SQL Hibernate log shows no delete query...after passing remove method.
Thanks,
If you do not dereference the token from User's list of tokens, cascade persist (you have cascade all set which includes persist) will cause the token to be resurrected at some point. You must clear all references to the child, especially ones marked cascade persist when you want to remove entities. Something like:
if ((token.getExpiryDate().compareTo(new GregorianCalendar()
.getTime())) == 1) {
user.getTokens().remove(token);
userService.activateUser(user);
//tokenService.deleteToken(token); //no longer needed due to orphanremoval being true
model.addAttribute("activationSuccess", "true");
...
In my Profile class I have
#OneToOne(cascade=CascadeType.PERSIST)
private ProfilePicture profilePic = null;
My method in updating the profilePic
public Profile updateUserProfilePic(Profile user) {
EntityManager em = EMF.get().createEntityManager();
em.getTransaction().begin();
Profile userx = em.find(Profile.class, user.getEmailAddress());
userx.setProfilePic( user.getProfilePic() );
em.getTransaction().commit();
em.close();
return userx;
}
When updateUserProfilePic is called, it just add another profilePic in datastore, it doesn't replaced the existing profilePic. Is my implementation correct? I want to update the profilePic of profile.
"Transient" means not persistent and not detached.
Using that version of GAE JPA you need a detached or managed object there if you want it to reuse the existing object.
Using v2 of Googles plugin there is a persistence property that allows merge of a transient object that has "id" fields set.
I am struggling while handling sessions in GAE. I am trying to store a two classes and a string in session. Although on DEV environment it runs fine, on production a class and a string are not being persisted in session. The class that is not getting saved as a session attribute is as follows:
#PersistenceCapable(detachable="true")
public class Agent implements Serializable{
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
#Persistent private String name; //Name of the Agency
#Element(dependent = "true")
private List<Contact> contacts = new ArrayList<Contact>();
#Element(dependent = "true")
private List<Agency> agencies = new ArrayList<Agency>();
#Persistent private List<Long> subAgents = new ArrayList<Long>();
#Persistent private Date createdOn = new Date();
}
I would like to mention again that it works fine on DEV Environment but on production I get values as null. As you can see I have made the class implement Serializable. But I think it is not the problem because I am setting one more attribute as a simple string and that also is failing (I get the attribute value as null). Session however is created as I can see it at the backend and also there is one more class which is persisted in session.
Anybody have suggestions? Thanks in advance.
Your problem is probably related to either:
GAE often serializes sessions almost immediately, dev environment doesn't. So all objects in your graph must implement Serializable.
BUT EVEN MORE LIKELY is that after you modify a session variable, you must do something like req.getSession().setAttribute(myKey,myObj) - it WILL NOT see changes in your object and automatically write them back to the session... so the session attributes will have the value of whatever they had when they were last set.
Problem #2 above cost me countless time and pain until I tripped over (via a lengthy process of elimination).
Have you enabled sessions in your configuration file?
http://code.google.com/intl/en/appengine/docs/java/config/appconfig.html#Enabling_Sessions
Making classes Agency and Contact Serializable solves the problem. That mean each and every object (be it nested or otherwise) which is present inside a session attribute should be serializable.