gpt4 book ai didi

java - 使用 HQL Select 的 Hibernate 和 JAX-RS 响应

转载 作者:行者123 更新时间:2023-11-30 10:44:07 25 4
gpt4 key购买 nike

所以我有以下内容。

我有一个名为 GroupEntity 的实体,它有一个 groupId、名称和一个 userId,它是与 UserEntity 对应的 id。

我想要的:带有 groupentity 和 userentity.name 的 JSON 响应。但出于某种原因,我收到了一个没有任何堆栈跟踪的 505 错误。

服务:

public GenericEntity<List<GroupEntity>> getMyGroups() throws HibernateException {
session.beginTransaction();
Query query = session.createQuery("FROM GroupEntity g inner join g.teacher as teacher");
List<GroupEntity> groups = (List<GroupEntity>) query.list();
GenericEntity<List<GroupEntity>> entity = new GenericEntity<List<GroupEntity>>(groups) {};

return entity;

}

JAX-RS 资源

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getMyGroups(){

return Response.ok(groupService.getMyGroups(),
MediaType.APPLICATION_JSON)
.build();

}

集团实体

    @Entity
@Table(name = "Group", schema = "pad_ijburg", uniqueConstraints = {
@UniqueConstraint(columnNames = "name")
})

public class GroupEntity implements Serializable {
private int idGroup;
private String name;

@XmlTransient
private UserEntity teacher;

public GroupEntity(String name, UserEntity teacher) {
this.name = name;
}

public GroupEntity(){

}

@XmlTransient
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idUser")
public UserEntity getTeacher() {
return teacher;
}

@XmlTransient
public void setTeacher(UserEntity teacher) {
this.teacher = teacher;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getIdGroup() {
return idGroup;
}

public void setIdGroup(int idGroup) {
this.idGroup = idGroup;
}

@Basic
@Column(name = "name")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

用户实体

    @Entity
@Table(name = "User", schema = "pad_ijburg", catalog = "", uniqueConstraints = {
@UniqueConstraint(columnNames = "email")
})
@Inheritance(strategy = InheritanceType.JOINED)
public class UserEntity implements Serializable{
private int idUser;

private String email;
private String firstName;
private String lastName;
//ToDo Make this password secure!
private String password;
private boolean admin;
private boolean teacher;

//Lists below

private Set<GroupEntity> teacherGroups = new HashSet<GroupEntity>(
0);



public UserEntity(String email, String firstName, String lastName, String password, boolean admin, boolean teacher) {
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
this.password = password;
this.admin = admin;
this.teacher = teacher;
}

public UserEntity() {

}

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idUser")
public int getIdUser() {
return idUser;
}

public void setIdUser(int idUser) {
this.idUser = idUser;
}

@Basic
@Column(name = "email")
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Basic
@Column(name = "firstName")
public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

@Basic
@Column(name = "lastName")
public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

@Basic
@Column(name = "password")
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Basic
@Column(name = "admin")
public boolean getAdmin() {
return admin;
}

public void setAdmin(boolean admin) {
this.admin = admin;
}

@Basic
@Column(name = "teacher")
public boolean getTeacher() {
return teacher;
}

public void setTeacher(boolean teacher) {
this.teacher = teacher;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "idGroup")
public Set<GroupEntity> getTeacherGroups() {
return teacherGroups;
}

public void setTeacherGroups(Set<GroupEntity> teacherGroups) {
this.teacherGroups = teacherGroups;
}
}

我认为这与正文编写器有关。问题是:我真的不知道如何解决这个问题。

正文错误:

29-May-2016 02:29:43.679 SEVERE [http-nio-8079-exec-9] org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=class java.util.ArrayList

更新:

我已经修复了 bodywriter。但我仍然收到 505 错误(没有堆栈跟踪)。我已经更新了上面的服务代码。

更新 2.0 POM.XML:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.hva.folivora</groupId>
<artifactId>api</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>IJBurg ReflectionAPP API</name>

<build>
<finalName>api</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.0.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>4.1.0.Final</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.2</version>
</dependency>

<!-- Ensures JSON Support for the RESTAPI -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>

<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.4</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>

</dependencies>
<properties>
<jersey.version>2.22</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

更新 3.0

session.beginTransaction();
Query query = session.createQuery("SELECT g.idGroup, g.name, teacher.idUser FROM GroupEntity g inner join g.teacher as teacher");
List<Object[]> groups = (List<Object[]>) query.list();

//GenericEntity<List<GroupEntity>> entity = new GenericEntity<List<GroupEntity>>(groups) {};

//return entity;

return groups;

以下将返回以下 json 响应。数据是正确的。但它没有以正确的格式返回。

    [
[
3,
"TestGroup3",
3
]
]

预先感谢您的帮助!

最佳答案

好的,我为您找到了一个快速的替代解决方案:

<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.4</version>
</dependency>

此依赖项为您希望以 JSON 格式传递的此类对象创建自动数据绑定(bind)。在你的情况下你想要一个 list通过。您可以找到有关 Genson here 的更多信息

关于java - 使用 HQL Select 的 Hibernate 和 JAX-RS 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37504766/

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