gpt4 book ai didi

java - 未使用 App Engine Java 在 JPA 中生成主键

转载 作者:行者123 更新时间:2023-12-01 13:19:06 25 4
gpt4 key购买 nike

我有实体类“User”,如下所示,但它不生成主键。我在我的应用程序引擎应用程序中使用 JPA 并在我的 Android 客户端中使用应用程序引擎端点。

实体类:

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

private String userName;
private String fullName;
private String userAvtarUrl;
private String userAbout;
private String userGender;


public String getUserName() {
return userName;
}

public Long getUserId() {
return userId;
}

public void setUserId(Long userId) {
this.userId = userId;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public String getUserAvtarUrl() {
return userAvtarUrl;
}

public void setUserAvtarUrl(String userAvtarUrl) {
this.userAvtarUrl = userAvtarUrl;
}

public String getUserAbout() {
return userAbout;
}

public void setUserAbout(String userAbout) {
this.userAbout = userAbout;
}

public String getUserGender() {
return userGender;
}

public void setUserGender(String userGender) {
this.userGender = userGender;
}
}

端点持久化代码:

@ApiMethod(name = "insertUserMaster")
public UserMaster insertUserMaster(UserMaster usermaster) {
EntityManager mgr = getEntityManager();
try {
if (containsUserMaster(usermaster)) {
throw new EntityExistsException("Object already exists");
}
mgr.persist(usermaster);
} finally {
mgr.close();
}
return usermaster;
}

Android客户端

Userendpoint.Builder builder = new Userendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(), new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) {
}
});

Userendpoint endpoint = CloudEndpointUtils.updateBuilder(
builder).build();

User objUser = new User();

objUser.setUserName(txtName.getText().toString());
objUser.setUserEmail(txtEmail.getText().toString());

Bitmap bmp = BitmapFactory.decodeFile(imagePath);
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
byte[] imgByte = out.toByteArray();
String encodedImage = Base64.encodeToString(imgByte,
Base64.DEFAULT);
objUser.setImage(encodedImage);

User result = endpoint.insertUser(objUser).execute();

请指导我哪里不足。谢谢。

最佳答案

如果使用 JPA 和 GAE/数据存储,请使用所有 JPA 注释,或者全部作为供应商扩展使用所有 JDO 注释。您不能混合搭配。

关于java - 未使用 App Engine Java 在 JPA 中生成主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22196669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com