我需要创建一个对象,该对象包含一个表的所有字段,并且来自另一个表的一些信息在一起(不是全部)。
@查询(“从用户U内部加入U.Promotor P中选择new com.nowigo.systemsheet.User.User(U.id,U.name),其中P.id=?1”)公共列表findAllById(长promoterId);
@查询(“从用户U内部加入U.P中选择U.id、U.name、P.name,其中P.id=?1”)
实体用户
package com.nowigo.systemsheet.User;
import com.nowigo.systemsheet.base.BaseEntity;
import com.nowigo.systemsheet.Promoter.Promoter;
import java.io.Serializable;
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.springframework.format.annotation.DateTimeFormat;
@Entity
//@Inheritance(strategy = InheritanceType.JOINED)
public class User extends BaseEntity implements Serializable{
@ManyToOne
@JoinColumn(name = "promoter_id" )
private Promoter promoter;
private String name;
private String username;
private String password;
private String description;
@Column(name = "enabled", columnDefinition = "tinyint")
private Boolean enabled;
private Integer types;
private String permissions;
private Long restricted_event_id;
private Long restricted_cashlesscash_id;
private Long restricted_reseller_id;
@Column(name = "time_begin_reseller_bat", columnDefinition = "timestamp")
@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm:ss")
private Date time_begin_reseller_bat;
@Column(name = "time_end_reseller_bat", columnDefinition = "timestamp")
@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm:ss")
private Date time_end_reseller_bat;
@Column(name = "time_begin_reseller_individual", columnDefinition = "timestamp")
@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm:ss")
private Date time_begin_reseller_individual;
@Column(name = "time_end_reseller_individual", columnDefinition = "timestamp")
@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm:ss")
private Date time_end_reseller_individual;
private Long img_id;
@Column(name = "can_make_return_operation", columnDefinition = "tinyint")
private Boolean can_make_return_operation;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Long getRestricted_reseller_id() {
return restricted_reseller_id;
}
public void setRestricted_reseller_id(Long restricted_reseller_id) {
this.restricted_reseller_id = restricted_reseller_id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public Integer getTypes() {
return types;
}
public void setTypes(Integer types) {
this.types = types;
}
public String getPermissions() {
return permissions;
}
public void setPermissions(String premissions) {
this.permissions = permissions;
}
public Long getRestricted_event_id() {
return restricted_event_id;
}
public void setRestricted_event_id(Long restricted_event_id) {
this.restricted_event_id = restricted_event_id;
}
public Long getRestricted_cashlesscash_id() {
return restricted_cashlesscash_id;
}
public void setRestricted_cashlesscash_id(Long restricted_cashlesscash_id) {
this.restricted_cashlesscash_id = restricted_cashlesscash_id;
}
public Long getRestricted_resellet_id() {
return restricted_reseller_id;
}
public void setRestricted_resellet_id(Long restricted_resellet_id) {
this.restricted_reseller_id = restricted_resellet_id;
}
public Date getTime_begin_reseller_bat() {
return time_begin_reseller_bat;
}
public void setTime_begin_reseller_bat(Date time_begin_reseller_bat) {
this.time_begin_reseller_bat = time_begin_reseller_bat;
}
public Date getTime_end_reseller_bat() {
return time_end_reseller_bat;
}
public void setTime_end_reseller_bat(Date time_end_reseller_bat) {
this.time_end_reseller_bat = time_end_reseller_bat;
}
public Date getTime_begin_reseller_individual() {
return time_begin_reseller_individual;
}
public void setTime_begin_reseller_individual(Date time_begin_reseller_individual) {
this.time_begin_reseller_individual = time_begin_reseller_individual;
}
public Date getTime_end_reseller_individual() {
return time_end_reseller_individual;
}
public void setTime_end_reseller_individual(Date time_end_reseller_individual) {
this.time_end_reseller_individual = time_end_reseller_individual;
}
public Long getImg_id() {
return img_id;
}
public void setImg_id(Long img_id) {
this.img_id = img_id;
}
public Boolean getCan_make_return_operation() {
return can_make_return_operation;
}
public void setCan_make_return_operation(Boolean can_make_return_operation) {
this.can_make_return_operation = can_make_return_operation;
}
public Boolean getCan_make_register_operation() {
return can_make_register_operation;
}
public void setCan_make_register_operation(Boolean can_make_register_operation) {
this.can_make_register_operation = can_make_register_operation;
}
@Column(name = "can_make_register_operation", columnDefinition = "tinyint")
private Boolean can_make_register_operation;
public Promoter getPromoter() {
return promoter;
}
public void setPromoter(Promoter promoter) {
this.promoter = promoter;
}
public User(String name) {
this.name = name;
this.getId();
}
public User() {
}
public User(Promoter promoter, String name, String username, String password, String description, Boolean enabled, Integer types, String permissions, Long restricted_event_id, Long restricted_cashlesscash_id, Long restricted_reseller_id, Date time_begin_reseller_bat, Date time_end_reseller_bat, Date time_begin_reseller_individual, Date time_end_reseller_individual, Long img_id, Boolean can_make_return_operation, Boolean can_make_register_operation) {
this.promoter = promoter;
this.name = name;
this.username = username;
this.password = password;
this.description = description;
this.enabled = enabled;
this.types = types;
this.permissions = permissions;
this.restricted_event_id = restricted_event_id;
this.restricted_cashlesscash_id = restricted_cashlesscash_id;
this.restricted_reseller_id = restricted_reseller_id;
this.time_begin_reseller_bat = time_begin_reseller_bat;
this.time_end_reseller_bat = time_end_reseller_bat;
this.time_begin_reseller_individual = time_begin_reseller_individual;
this.time_end_reseller_individual = time_end_reseller_individual;
this.img_id = img_id;
this.can_make_return_operation = can_make_return_operation;
this.can_make_register_operation = can_make_register_operation;
}
}
实体发起人
package com.nowigo.systemsheet.Promoter;
import com.nowigo.systemsheet.User.User;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nowigo.systemsheet.base.BaseEntity;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
@Entity
//@PrimaryKeyJoinColumn(name="user_id")
public class Promoter extends BaseEntity implements Serializable{
@OneToMany(mappedBy = "promoter", orphanRemoval = true)
@JsonIgnore
private List<User> user;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String address;
private String address_number;
private String address_extra;
private String address_area;
private String city;
private String cnpx;
private String ierg;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress_number() {
return address_number;
}
public void setAddress_number(String address_number) {
this.address_number = address_number;
}
public String getAddress_extra() {
return address_extra;
}
public void setAddress_extra(String address_extra) {
this.address_extra = address_extra;
}
public String getAddress_area() {
return address_area;
}
public void setAddress_area(String address_area) {
this.address_area = address_area;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCnpx() {
return cnpx;
}
public void setCnpx(String cnpx) {
this.cnpx = cnpx;
}
public String getIerg() {
return ierg;
}
public void setIerg(String ierg) {
this.ierg = ierg;
}
public List<User> getUser() {
return user;
}
public void setUser(List<User> user) {
this.user = user;
}
public Promoter(String address) {
this.address = address;
}
public Promoter() {
}
public Promoter(List<User> user, String password, String address, String address_number, String address_extra, String address_area, String city, String cnpx, String ierg) {
this.user = user;
this.password = password;
this.address = address;
this.address_number = address_number;
this.address_extra = address_extra;
this.address_area = address_area;
this.city = city;
this.cnpx = cnpx;
this.ierg = ierg;
}
}
用户存储库
package com.nowigo.systemsheet.User;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface UserRepository extends JpaRepository<User, Long> {
public List<User> findByPromoterId(Long promoterId);
@Query(value = "SELECT U.id,U.name, P.address FROM sheetsystem.user U JOIN sheetsystem.promoter P ON U.promoter_id = P.id where P.id = ?1", nativeQuery = true)
public List<User> findAllById(Long promoterId);
}
预期:
{
"id": 17,
"promoter": {
"id": 4,
"address": "Av. John Wick"
},
"name": "usuario",
"username": null,
"description": null,
"enabled": null,
"types": null,
"permissions": null,
"restricted_event_id": null,
"restricted_cashlesscash_id": null,
"restricted_reseller_id": null,
"time_begin_reseller_bat": null,
"time_end_reseller_bat": null,
"time_begin_reseller_individual": null,
"time_end_reseller_individual": null,
"img_id": null,
"can_make_return_operation": null,
"can_make_register_operation": null,
"restricted_resellet_id": null
}
我明白了:
没有找到can_make_register_operation
我不认为使用实体类从本机查询返回部分水合的对象是好的做法。一旦将该实例放入businses层,您可能会失去一些上下文,无法知道它是否可以在以后持久化,或者它是否是不可变的,因为从技术上讲,它是一个带注释实体的实例。您可能会面临这样的风险:有人可能会意外地将对数据存储的更改与松散的数据合并。
就我个人而言,我认为更好的做法是设计一个中间对象,持久化层返回一个唯一适合您所追求的数据的中间对象,该对象清楚地标识返回的对象是不可变的,不能更改,也不能持久化,如下所示:
public class UserProfileAddress {
private Integer id;
private String name;
private Promoter promoter;
UserProfileAddress(Integer id, String name, Promoter promoter) {
this.id = id;
this.name = name;
this.promoter = promoter;
}
// .. getter/setters
}
然后,您应该能够将查询编写为
SELECT new com.package.UserProfileAddress( u.id, u.name, u.promoter )
FROM User u
JOIN FETCH u.promoter
WHERE u.promoter.id = :promoterId
现在您使用的是可移植的JPA查询而不是本机查询,这使得您的代码在您决定从一个数据存储更改为另一个数据存储时更能适应未来的潜在更改。
至于你对预期的错误;你需要提供更多的上下文。
是否可以声明性地在类的字段上指定验证约束,以便其中一些约束只适用于某些层(或者被某些层忽略)? 其中是JSR 303 anotation REST API层 此处不存在 null
问题内容: 我在Spring Boot应用程序中使用一种应该异步运行的方法来做一个类。当我阅读方法时,应该加上注释,而且我还必须运行一个bean。但是在Spring手册http://docs.spring.io/spring/docs/current/spring- framework- reference/html/scheduling.html中, 我找不到任何信息或示例如何在没有XML配置的
问题内容: 我有以下四个表: SCHEDULE_REQUEST表: ID,APPLICATION_ID(FK) 应用表: ID,代码 USER_APPLICATION表: APPLICATION_ID(FK),USER_ID(FK) 用户表: ID,NAME 现在,我想创建一个条件条件,以便为指定的用户ID 选择。 我有以下代码: 您能帮我如何将查询过滤器传递给关系对象吗?我认为我在 “ appl
问题内容: 给定以下类层次结构,我希望根据类层次结构中使用的上下文,对Foo进行不同的序列化。 我希望序列化FooContainer时biz属性不显示在fooB中。因此,输出将类似于以下内容。 我本打算使用JsonView,但是必须将其应用于类的所有实例的映射器层,并且这取决于上下文。 在Jackson用户的邮件列表中,Tatu提供了最简单的解决方案(在2.0中可用),我现在可能最终会使用它。将奖
主要内容:Python类的实例化,Python类对象的使用通过前面章节的学习,我们已经学会如何定义一个类,但要想使用它,必须创建该类的对象。 创建类对象的过程,又称为类的实例化。 Python类的实例化 对已定义好的类进行实例化,其语法格式如下: 类名(参数) 定义类时,如果没有手动添加 __init__() 构造方法,又或者添加的 __init__() 中仅有一个 self 参数,则创建类对象时的参数可以省略不写。 例如,如下代码创建了名为 CLang
我试图创建一个使用的组合注释,但是我似乎无法使其工作。 这是有效的(使用): 但是,直接使用不起作用(大概是由于只允许在方法上使用): 无法将用作可组合注释吗?