Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 546351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:51:29+00:00 2026-05-13T10:51:29+00:00

I’m still fairly new to working with java and the google appengine datastore. I

  • 0

I’m still fairly new to working with java and the google appengine datastore.

I can put data in and get it out of the datastore, and I am trying to make it so that a user cannot be entered twice.
As there is no unique index on the datastore, I’m setting a hash of the users email address as a primarykey.

Strangely, when I enter the same data multiple times, it is being entered into the datastore (which I thought would have returned an error, or done nothing).

So when I set my emailhash to ‘2’ for testing, and then run the insert script a bunch of times, and the query WHERE _emailHash=’2′, I get 3 results.

Here is the class where I am defining the user.

@Entity
public class user
{
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long _uid;

 @PrimaryKey
 private String _emailHash;

 private String _firstName;

 private String _lastName;

 private String _email;

 private String _password;

 public Long getUid()
 {
  return _uid;
 }

 public String getEmailHash(){
  return _emailHash;
 }
 public void setEmailHash(String emailHash)
 {
  _emailHash = emailHash;
 }

 public String getFirstName()
 {
  return _firstName;
 }
 public void setFirstName(String firstName)
 {
  _firstName = firstName;
 }
 public String getLastName()
 {
  return _lastName;
 }
 public void setLastName(String lastName)
 {
  _lastName = lastName;
 }

 public String getEmail()
 {
  return _email;
 }
 public void setEmail(String email)
 {
  _email = email;
 }
 public String getPassword()
 {
  return _password;
 }
 public void setPassword(String password)
 {
  _password = password;
 }
}

The google documentation says the following

an entity ID ("key name") provided by the application when the object is created. Use this for objects without entity group parents whose IDs should be provided by the application. The application sets this field to the desired ID prior to saving.
import javax.jdo.annotations.PrimaryKey;

// ...
    @PrimaryKey
    private String name;

at
http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html

Is there a better way to guarantee uniques? Or do I have to check if the value exists each time before inserting?

————————–UPDATE———————————————-
As per Dmitri’s response, I was mixing JPA and JDO (or at least getting confused between the two). Now that I’ve got that sorted out, my hashed email definition looks like this

@Id
 @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
 private String _emailHash;

 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long _uid;

Unfortunately when trying to create a user with

$pm = EMF::createEntityManager();
 $user = new user();
 $user->setEmailHash('5');
 $user->setFirstName('test5');
 $user->setLastName('test5');
 $user->setEmail('test5');

 $pm->persist($user);

I get the following error

Invalid primary key for com.nextweeq.scheduler.user. The primary key field is an encoded String but an unencoded value has been provided

So far my searches are returning something about specifying the keyname, but I haven’t quite found the solution yet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:51:29+00:00Added an answer on May 13, 2026 at 10:51 am

    Documentation you are referring to is about JDO, while your code seems to be using JPA where @Id plays the same role as @PrimaryKey plays in JDO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 385k
  • Answers 385k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When validation fails for a model property - it'll add… May 14, 2026 at 11:38 pm
  • Editorial Team
    Editorial Team added an answer ... ul li a { text-decoration: none; color: #666; display:… May 14, 2026 at 11:37 pm
  • Editorial Team
    Editorial Team added an answer Your function is Θ(log n · n): The outer loop… May 14, 2026 at 11:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.