ejb3 toplink jpa 1.0查询和id序列策略(ejb3 toplink jpa 1.0 querying and id sequence strategy)

我有两个问题:假设我们有一个名为class的实体,另一个叫做student。 每个班级都有学生。

public class Clas implements Serializable {
@Id
  @GeneratedValue(strategy=GenerationType.SEQUENCE)
private int id;
@OneToMany(cascade=CascadeType.ALL)
Collection<Student> students;
public clas(){
super();
}
 ..... getters and setters
}

q1:我得到异常,由于序列策略没有要映射的字段,当添加任何其他列如String name时,它可以工作,但我不需要该字段我该怎么办?

Q2。 ids是自动生成的,我想查询类c1中的所有学生,但我没有这个类的id,如何在不使用id的情况下获得这样的查询? 或者如何获取数据库实体id来查询它?

我使用mysql服务器glassfish v2.1 toplink jpa 1.0

谢谢


I have 2 questions: suppose we have one entity named class and another called student. each class has onetomany students.

public class Clas implements Serializable {
@Id
  @GeneratedValue(strategy=GenerationType.SEQUENCE)
private int id;
@OneToMany(cascade=CascadeType.ALL)
Collection<Student> students;
public clas(){
super();
}
 ..... getters and setters
}

q1: i get the exception there are no fields to be mapped because of sequence strategy, when adding any other column like String name, it works, but i don't need that field what can i do ?

q2. the ids is autogenerated, and i want to query all students in class c1, but i don't has the id of this class, how to get such query without using id? or how to get database entity id to query on it ?

iam working with mysql server glassfish v2.1 toplink jpa 1.0

Thanks


原文:https://stackoverflow.com/questions/8635695
2022-01-25 14:01

满意答案

您可以使用:在重复键更新名称=值(名称),firstname = values(firstname)上插入table_name(id,name,firstname)值(1,“Sessi”,“Brahim”)

使其适应您的查询。


You can use: insert into table_name (id, name, firstname) values(1, "Sessi", "Brahim") on duplicate key update name=values(name), firstname=values(firstname)

Adapt it to your query.

相关问答

更多

插入如果不存在MySQL(Insert into if not exist MySQL)

这是你的代码。 IF语句用于比较,你的只是获取数组,你的IF语句中没有任何条件。 您需要修复语句以获取列及其各自的值。 $check_for_episode = mysqli_query($conn, "SELECT DISTINCT house_number,number FROM episode WHERE house_number LIKE '".$house_number."' AND number = ".$episode_id_pg." LIMIT 1"); if (mysqli...

如何插入数据,如果使用coldfuison数据库中不存在该数据?(How to insert data, If that data does not exist in database using coldfuison?)

<cfquery name="qGetRecord" datasource="yourSorce"> <!--- Your SELECT Query for finding the data---> </cfquery> <cfif qGetRecord.recordCount EQ 0> <!--- Your INSERT Query ---> <cfelse> <!--- Show eroor message ---> </cfif> 逻辑是,首先我们将尝试获取...

表不存在于数据库中但它确实存在(Table doesn't Exist in database but it does)

可能是冲突.. 2个并发请求,因此一个请求可以删除另一个请求使用的表 尝试将CREATE TABLE更改为CREATE TEMPORARY TABLE May be conflicts.. 2 simultaneous requests, so one request can drop table that is used by another request Try to change CREATE TABLE to CREATE TEMPORARY TABLE

如果不存在,则将数据插入mysql数据库(Insert data to mysql database if not exist)

您可以使用:在重复键更新名称=值(名称),firstname = values(firstname)上插入table_name(id,name,firstname)值(1,“Sessi”,“Brahim”) 使其适应您的查询。 You can use: insert into table_name (id, name, firstname) values(1, "Sessi", "Brahim") on duplicate key update name=values(name), firstna...

无法将数据插入MySQL数据库(Cannot insert data into MySQL Database)

尝试这个: INSERT INTO users (username, first_name, last_name, email, password, sign_up_date, activated) VALUES ('$un','$fn','$ln','$em','$pswd','$d','0')` 如果您的id列是自动增量,则不需要自己填写。 对于您的日期,您可以查看此链接以获取日期参考。 date结构化列需要YY-MM-DD格式,您可以通过以下方式获取: $d = date("Y-m-d")...

如果值不存在,如何将数据插入SQLite数据库?(How to insert data into a SQLite Database only if the value does not exist?)

使值唯一,然后在插入期间检查数据库值是否已存在。 喜欢这个 : 检查方法: public boolean dataExists(int id) { SQLiteDatabase db = this.getReadableDatabase(); String Query = "SELECT * FROM " + TABLE_NAME + " WHERE " + KEY_QUESION_ID + "=" + id; Cursor cursor = db....

使用PHP非空的输入将数据插入MySQL数据库(Insert data into MySQL database from inputs that are not empty with PHP)

你在那里做了很多插页! 你应该只做一次插入! 尝试使用以下内容替换else语句: else { if($userDesc AND $userLocation AND $userBirthDate AND $user_picture_name != '') { if($userDesc = ''){$userDesc = 'Placeholder description';} if($userLocation = ''){$userLocation = 'P...

如果数据不存在(不包括主键),则插入MySQL(MySQL insert if data doesn't exist(excluding primary key))

您只需在描述字段中添加UNIQUE约束即可。 然后在代码中使用“INSERT ... ON DUPLICATE KEY UPDATE”。 请参阅此链接 。 You would simply add a UNIQUE constraint to the description field. and then use "INSERT ... ON DUPLICATE KEY UPDATE" in your code. Refer this link.

如果不存在则插入数据(Insert data if not exist)

正确的语法是INSERT IGNORE INTO INSERT IGNORE INTO ref_users (id) VALUES ('$id') 如果值不存在,它将插入,如果存在,则忽略该语句。 请注意,这仅在id键时才有效 编辑:从您的评论看来,使用ON DUPLICATE KEY UPDATE会更好。 试试这个查询 INSERT INTO ref_users(id, ref_code) VALUES ('$id', '$ref_code') ON DUPLICATE KEY UPDATE ...

相关文章

更多

JPA ORM框架介绍

标签定义的属性(在表中的字段)

【第八章】 对ORM的支持 之 8.4 集成JPA ——跟我学spring3

JPA全称为Java持久性API(Java Persistence API),JPA是Java EE ...

hibernate id 生成策略及主要使用方法

hibernate主键策略生成器 hibernate提供多种主键生成策略,有点是类似于JPA,有的是h ...

Hibernate Search(基于version3.4)--第五章Querying

Hibernate Search的第二个很重要的能力是运行Lucene queries并通过Hiber ...

JPA高级映射(继承)

TeachingStaff和NonTeachingStaff是Staff的子类

JPA一对多、一对一、多对一、多对多实体关系

创建第二个实体这种关系的Employee实体类

JPA 增删改查入门示例

它记录在 persistence.xml 文件数据库中并创建数据库

传智播客JPA详解视频教程

本套视频教程是黎活明老师在课堂上给传智播客学员专门讲解的JPA课程,由于传智播客学员在学习JPA课程前 ...

spring 3.0 和jpa 整合 用jboss

1、网上说了很多例子,这就不说了,只介绍下我遇见的问题 java.lang.ClassCastExce ...

JPA标准API-JPQL查询的另一种方式

javax.persistence.Persistence

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本