我有一个Cat类和一个Owner类.一只猫有一个主人,但主人可以有很多猫.我想要查询的是让所有拥有蓝眼睛的猫的主人.
class Cat {
Owner owner; //referenced from Owner.id
String eyeColor;
}
class Owner {
List<Cat> catList;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了一些代码,但我真的不知道该怎么做.
Criteria criteria = getCurrentSession().createCriteria(cat.getClass(), "cat");
criteria.createAlias("cat.owner", "owner");
criteria.add(Restrictions.eq("cat.eyeColor", "blue");
Run Code Online (Sandbox Code Playgroud) 如何将OutputStream转换为字节数组?我发现首先我需要将此OutputStream转换为ByteArrayOutputStream.此OutputStream类中只有write()方法,我不知道该怎么做.有什么想法吗?
我有近30个课程,我想将这些课程应用到我的按钮元素.我不想为每个按钮元素添加class属性.有没有办法创建一个新的按钮类,如;
.button{
.rounded-corner
.corner
.button-effective
//another 20 classes
}
Run Code Online (Sandbox Code Playgroud) 我有一个名为 Task 的实体,它有 taskId 字段。问题是我需要创建/更新一些特定任务,而 JPA 自动生成器不允许我手动设置任务 ID。(它会覆盖我的任务 ID)
有没有办法手动设置taskId?
@Id
@Column(name = "task_id")
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "org.hibernate.id.UUIDGenerator")
private String taskId;
Run Code Online (Sandbox Code Playgroud) 我刚刚创建了一个2人游戏(比如ShapeGame),但问题是当其中一个玩家离开游戏场景时,我无法察觉哪一个(哪个玩家)从游戏中离开.
认为游戏中有2辆车.首先检测到的玩家(称之为player1)使用左侧玩家而玩家2使用右侧玩家.当player1离开场景时,突然player2控制左车,如果player1重新加入游戏,则player1再次取回对左车的控制权,并且player2控制右车.
int id = 0;
foreach (SkeletonData data in skeletonFrame.Skeletons)
{
if (SkeletonTrackingState.Tracked == data.TrackingState)
{
// do some work
id++;
}
}
Run Code Online (Sandbox Code Playgroud)
事情是id = 0第一个检测到的玩家(称之为player1)的程序给出,第二个被检测的玩家(称之为player2)为1.当player2从游戏中退出并重新加入时,没有问题.它再次需要id = 1.但是当玩家1离开游戏时,玩家2 id = 0因为它是此时第一个被检测到的玩家.
有解决方案吗
我想为 postgresql 设置默认架构。当我尝试设置 property("hibernate.default_schema") 并使用 schema 属性更新表注释时,hibernate 仍然会生成没有任何模式名称的查询,如下所示;
select log0_.id as id1_0_ from log log0_
Run Code Online (Sandbox Code Playgroud)
当我将表名更改为“dbo.log”时,我可以获得结果集而不会出现任何错误。
@Table(name = "dbo.log")
Run Code Online (Sandbox Code Playgroud)
有没有办法为 postgresql 查询设置默认模式?
@Configuration
@EnableJpaRepositories(
basePackages = "com.test.repository.postgresql",
entityManagerFactoryRef = "postgresqlEntityManagerFactory",
transactionManagerRef = "postgresqlTransactionManager"
)
public class PostgreSQLConfig {
....
@Bean(name = "postgresqlEntityManagerFactory")
public EntityManagerFactory entityManagerFactory(@Qualifier("postgresqlDatasource") DataSource postgresqlDatasource) {
Properties jpaProperties = new Properties();
jpaProperties.setProperty("hibernate.dialect", hibernateDialect);
jpaProperties.setProperty("hibernate.show_sql", hibernateShowSql);
jpaProperties.setProperty("hibernate.ddl-auto", hibernateDDLAuto);
jpaProperties.setProperty("hibernate.naming.physical-strategy", hibernatePhysicalStrategy);
jpaProperties.setProperty("hibernate.default_schema", "dbo");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.POSTGRESQL);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setJpaProperties(jpaProperties);
factory.setPackagesToScan("com.test.entity.postgresql");
factory.setDataSource(postgresqlDatasource);
factory.afterPropertiesSet();
return …Run Code Online (Sandbox Code Playgroud) 我有ap:对话框,里面有一个面板.问题是"保存"按钮的操作方法不起作用.它甚至不调用该方法.我可以达到方法def.使用ctrl + lm,因此方法名称没有问题.
<h:body>
<h:form id="createAppUserForm" prependId="false">
....
<p:dialog id="newRoleDialogId"
header="Add New Role"
resizable="true"
draggable="true"
widgetVar="newRoleDetailsDialog"
appendToBody="true"
>
<p:panel id="newRoleDialogPanel">
<p:panelGrid id="newRoleDialogPanelGrid" columns="2" style="width: 100%" styleClass="panelGridWithoutBorder">
<h:outputText value="Role Name :"/>
<p:inputText value="#{createAppUserController.selectedRole.name}"/>
<h:outputText value="Role Desc :"/>
<p:inputText value="#{createAppUserController.selectedRole.description}"/>
</p:panelGrid>
<center>
<p:commandButton value="Save"
update="roleListDataTable newRoleDialogPanelGrid growlCreateAppUser"
oncomplete="if (!args.validationFailed) newRoleDetailsDialog.hide()"
action="#{createAppUserController.saveNewRole()}"/>
<p:commandButton value="Cancel"
immediate="true"
onclick="newRoleDetailsDialog.hide()" />
</center>
</p:panel>
</p:dialog>
</h:form>
</h:body>
Run Code Online (Sandbox Code Playgroud) 我在学校和学生实体之间建立了OneToMany关系.我想要做的是当我保存学校对象时不保存或更新学生对象.(当然不要删除它们)
当我尝试保存像下面的学校对象时,它也会更新我的学生对象,但我不希望它们更新但只能加入.有什么办法吗?
我删除了Cascade但它仍然无法正常工作.
School school = new School();
school.setStudents(studentList);
repository.save(school);
Run Code Online (Sandbox Code Playgroud)
我的实体;
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "school_student", joinColumns = {@JoinColumn(name = "school_id")},
inverseJoinColumns = {@JoinColumn(name = "student_id")})
private List<Student> students;
Run Code Online (Sandbox Code Playgroud)
编辑:
我想编写一个方便的扩展来从 Map 中提取值,同时解析它们。如果解析失败,函数应该返回一个默认值。这一切正常,但我想告诉 Kotlin 编译器,当默认值不为空时,结果也不会为空。我可以通过@Contract注释在 Java 中做到这一点,但它似乎在 Kotlin 中不起作用。这能做到吗?合同不适用于扩展功能吗?这是 kotlin 尝试:
import org.jetbrains.annotations.Contract
private const val TAG = "ParseExtensions"
@Contract("_, !null -> !null")
fun Map<String, String>.optLong(key: String, default: Long?): Long? {
val value = get(key)
value ?: return default
return try {
java.lang.Long.valueOf(value)
} catch (e: NumberFormatException) {
Log.e(TAG, e)
Log.d(TAG, "Couldn't convert $value to long for key $key")
default
}
}
fun test() {
val a = HashMap<String, String>()
val something: Long = a.optLong("somekey", 1)
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中, …
我想用Jackson来反序列化json,我想将空字符串映射到STANDARD枚举类型。
当我尝试将JsonProperty与空字符串一起使用时,它将忽略空值并引发异常;
value not one of declared Enum instance names:......,STANDARD,...
Run Code Online (Sandbox Code Playgroud)
有什么办法可以解决这个问题?
public enum Type{
@JsonProperty("")
STANDARD,
@JsonProperty("complex")
COMPLEX,
....
}
Run Code Online (Sandbox Code Playgroud)
我的json;
....
"type": "",
....
Run Code Online (Sandbox Code Playgroud)