简体   繁体   English

操作数类型冲突:datetime2 与 tinyint 不兼容

[英]Operand type clash: datetime2 is incompatible with tinyint

I was not able to solve my problem with existing information我无法用现有信息解决我的问题

Operand type clash: datetime2 is incompatible with tinyint

This problem occurs only when using mssql, Hope someone already knows the solution to this problem此问题仅在使用mssql时出现,希望有人已经知道此问题的解决方案

When switching to mssql there were a lot of problems, it's still only one of them, because some of I managed to solve切换到mssql时出现了很多问题,它仍然只是其中之一,因为我设法解决了一些问题

@Entity
@Table
public class DoseAssignment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Integer id;

@NotNull
@Size(min = 2, max = 30)
@Column(length = 30, nullable = false)
private String number;

@NotNull
@Column(columnDefinition = "tinyint", nullable = false)
private Integer unit;

@Temporal(TemporalType.DATE)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", 
timezone = "UTC")
private Date date;

@NotNull
@Column(nullable = false)
@Temporal(TemporalType.DATE)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", 
timezone = "UTC")
private Date dateOpenPlan;

@NotNull
@Column(nullable = false)
@Temporal(TemporalType.DATE)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", 
timezone = "UTC")
private Date dateClosePlan;

@Temporal(TemporalType.DATE)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", 
timezone = "UTC")
private Date dateOpen;

@Temporal(TemporalType.DATE)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", 
timezone = "UTC")
private Date dateClose;

@ManyToOne(fetch = FetchType.LAZY)
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Personal givePersonal;

@ManyToOne(fetch = FetchType.LAZY)
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private WorkMode workMode;

@ManyToOne(fetch = FetchType.LAZY)
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private ProgramDangerous program;

@NotNull
@Size(min = 2, max = 100)
@Column(length = 100, nullable = false)
private String content;

Controller控制器

Page<Integer> page = repository.findId(department == null ? null : new Department(department), number, unit, workMode == null ? null : new WorkMode(workMode), apartment == null ? new Integer[]{0} : apartment, personal == null ? new Integer[]{0} : personal, program, access, outer, dateOpenPlanFrom, dateOpenPlanTo, dateOpenFrom, dateOpenTo, dateAccessFrom, dateAccessTo, protectionEquipment  == null ? new Integer[]{0}: protectionEquipment, status, pageable);

Query询问

@Query("select a.id from DoseAssignment a where (:id is null or a.id=:id) and (:department is null or a.department=:department) and (:unit is null or a.unit=:unit) and (:workMode is null or a.workMode=:workMode) and (:program is null or :program=false and a.program is null or :program=true and a.program is not null) and (:dateOpenPlanFrom is null or a.dateOpenPlan>=:dateOpenPlanFrom) and (:dateOpenPlanTo is null or a.dateOpenPlan<=:dateOpenPlanTo) and (:dateOpenFrom is null or a.dateOpen>=:dateOpenFrom) and (:dateOpenTo is null or a.dateOpen<=:dateOpenTo) and (:status is null or a.status=:status) and (:access is null or :access=true and a.status=4 or :access=false and not a.status=4) and ((not 0 in :apartment or not 0 in :personal or :outer is not null or :dateAccessFrom is not null or :dateAccessTo is not null or not 0 in :protectionEquipment) and a.id in (select distinct ai.id from DoseAssignment ai left join ai.protectionEquipment pei left join ai.personal pi left join ai.apartment api left join ai.assignmentAccess aai where (:department is null or ai.department=:department) and (:unit is null or ai.unit=:unit) and (:workMode is null or ai.workMode=:workMode) and (:program is null or :program=false and ai.program is null or :program=true and ai.program is not null) and (:dateOpenPlanFrom is null or ai.dateOpenPlan>=:dateOpenPlanFrom) and (:dateOpenPlanTo is null or ai.dateOpenPlan<=:dateOpenPlanTo) and (:dateOpenFrom is null or ai.dateOpen>=:dateOpenFrom) and (:dateOpenTo is null or ai.dateOpen<=:dateOpenTo) and (:status is null or ai.status=:status) and (0 in :personal or pi.id in :personal) and (0 in :apartment or api.id in :apartment) and (0 in :protectionEquipment or pei.id in :protectionEquipment) and (:dateAccessFrom is null or aai.enterDate>=:dateAccessFrom) and (:dateAccessTo is null or aai.enterDate<=:dateAccessTo) and (:outer is null or :outer=true and pi.type=2 or :outer=false and a.id not in (select distinct aii.id from DoseAssignment aii left join aii.personal pii where pii.type=2))) or not (not 0 in :apartment or not 0 in :personal or :outer is not null or :dateAccessFrom is not null or :dateAccessTo is not null or not 0 in :protectionEquipment))")

. .

Page<Integer> findId(@Param("department") Department department, @Param("id") Integer id, @Param("unit") Integer unit, @Param("workMode") WorkMode workMode, @Param("apartment") Integer[] apartment, @Param("personal") Integer[] personal, @Param("program") Boolean program, @Param("access") Boolean access, @Param("outer") Boolean outer, @Param("dateOpenPlanFrom") Date dateOpenPlanFrom, @Param("dateOpenPlanTo") Date dateOpenPlanTo, @Param("dateOpenFrom") Date dateOpenFrom, @Param("dateOpenTo") Date dateOpenTo, @Param("dateAccessFrom") Date dateAccessFrom, @Param("dateAccessTo") Date dateAccessTo, @Param("protectionEquipment") Integer[] protectionEquipment, @Param("status") Integer status, Pageable pageable);

A datetime2 type has a size of 6-8 bytes (and contains date and time portions of a date) vs a tinyint which is 1 byte and contain values of 0-255. datetime2 类型的大小为 6-8 个字节(并包含日期的日期和时间部分),而 tinyint 为 1 个字节并包含 0-255 的值。 You cannot fit a ten lb weight into a 1 ounce container.您无法将 10 磅的重量放入 1 盎司的容器中。

The error gives the answer here.错误在这里给出了答案。 tinyint and datetime2 are incompatible. tinyintdatetime2不兼容。 If you want to convert a tinyint to a datetime2 , you'll need to firstly convert it to datetime (which is compatible with a tinyint ), and then to datetime2 .如果要将tinyint转换为datetime2 ,则需要先将其转换为datetime (与tinyint兼容),然后再转换为datetime2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM