我有这个LINQ选择:
var extendedPhrases = phrases
.Select(x => new ExtendedPhrase()
{
Ajlpt = x.Ajlpt,
Bjlpt = x.Bjlpt,
Created = x.Created // an int?
});
如果我定义:
public int? CreatedLast { get; set; }
那么如何用x.Created的最后一位填充呢?
答案 0 :(得分:5)
如果您要查找**Can not set int field com.example.domain.Company.cmpId to java.lang.Integer**
Hibernate except foreign key of employee table cmp_id, should be primary key of company. But in your code cmpId is not primary key.
@Column(name="company_id")
private int cmpId;
Please make that primary and check once.
Edited:-
1.
@OneToOne(fetch=FetchType.LAZY, mappedBy="company")
private Employee employee;
change to:-
@OneToOne(fetch=FetchType.LAZY, mappedBy="company",optional=false)
private Employee employee;
2.
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="cmp_id", referencedColumnName="company_id")
private Company company;
change to:-
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="cmp_id")
private Company company;
属性的最后一位,请使用% operator,如下所示:
Created
答案 1 :(得分:-1)
首先要想到的是调用.ToString()。Last():
var extendedPhrases = phrases
.Select(x => new ExtendedPhrase()
{
Ajlpt = x.Ajlpt,
Bjlpt = x.Bjlpt,
Created = x.Created,
CreatedLast = x.Created?.ToString().Last()
});
如果您没有使用最新的闪亮C#,则可以使用以下方式进行空保护:
var extendedPhrases = phrases
.Select(x => new ExtendedPhrase()
{
Ajlpt = x.Ajlpt,
Bjlpt = x.Bjlpt,
Created = x.Created,
CreatedLast = x.Created.HasValue ? x.Created.ToString().Last() : null
});
将一些转换回int?
作为练习留给读者。