我已经定义了我的两个实体类User和Permission之间的多对多关系.用户具有username和countyId的主键组合,我的Permission表具有常规整数Id.表UserPermission具有三个外键作为其主键:username,countyId和permissionId.
由于这是一个遗留数据库,我将无法使用Right Thing(™)并在User上创建一个整数主键.
我在User.class中定义了这样的多对多关系:
@ManyToMany(targetEntity=Permission.class, cascade={ CascadeType.PERSIST, CascadeType.MERGE } )
@JoinTable(name="tblUserPermission",
joinColumns = { @JoinColumn(name="username"), @JoinColumn(name="countyId") },
inverseJoinColumns = { @JoinColumn(name="permissionId") })
private Collection<Permission> permissions;
Run Code Online (Sandbox Code Playgroud)
Permission.class说:
@ManyToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "permissions", targetEntity = User.class )
private Collection<User> users;
Run Code Online (Sandbox Code Playgroud)
我认为这是要走的路,但是当我启动使用Hibernate 3的Spring上下文时,我得到:
Caused by: org.hibernate.AnnotationException: A Foreign key refering com.mydomain.data.entities.User from com.mydomain.data.entities.Permission has the wrong number of column. should be 1
Run Code Online (Sandbox Code Playgroud)
我在注释中做错了什么?应该是2,而不是1.
更新:
Arthur建议我添加referencedColumnName,但这给了我一个新的异常:
Caused by: org.hibernate.AnnotationException: referencedColumnNames(username, countyId) of com.mydomain.data.entities.Permission.permissions referencing com.mydomain.data.entities.User not mapped to a single property …Run Code Online (Sandbox Code Playgroud) 使用Spring进行单元测试的最佳实践方法是什么?我假设SpringN的组合TestNG和jmockit并不坏,所以这就是我现在正在做的事情,但如果我当然没有为我的新Spring项目选择工具,请马上告诉我.:-)
无论如何,我已经创建了一个我想测试的实体,但我不确定如何从Spring上下文中实际运行TestNG.我创建了一个简单的测试类
package com.mydomain.data.entities.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.*;
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class SimpleTest extends AbstractTestNGSpringContextTests {
@Autowired
private ApplicationContext applicationContext;
@BeforeClass
protected void setUp() throws Exception {
Assert.assertNotNull(applicationContext);
}
@Test
public void testNothing() {
}
}
Run Code Online (Sandbox Code Playgroud)
使用applicationContext.xml导入bean以设置模型和业务层.首先,我希望它使用Project/WebRoot/WEB-INF中的applicationContext.xml,而此测试的源代码位于Project/src/com/mydomain/data/entities/test中,但我相信/applicationContext.xml会给我Projects/src,和classpath一样:这是正确的吗?
此外,既然我正在使用我的Web应用程序将使用的模型和业务层,我应该期望它的行为类似.但是,当我在测试中启动TestNG时,它是sais:class org.hibernate.cfg.ExtendedMappings将接口org.hibernate.cfg.Mappings作为超类:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/Users/niklas/Documents/Eclipse/Project/WebRoot/WEB-INF/Project-Model.xml]: Invocation of init method failed; nested exception is java.lang.IncompatibleClassChangeError: class org.hibernate.cfg.ExtendedMappings has interface org.hibernate.cfg.Mappings as super class
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1393)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511) …Run Code Online (Sandbox Code Playgroud) 我对ScalaTest相当新,现在我已经让它与Maven一起运行了,当然我也想让它在Eclipse中运行良好.我的项目是一个Java项目,但我希望通过使用ScalaTest编写测试来提高我的Scala技能.
我理解它,所以我应该右键单击我的项目,说"配置"和"添加Scala自然".但是,这样做会让Eclipse尝试用scalac编译我的所有Java文件,在问题列表中给出了很多"Scala Problem"条目.当然,没有Scala特性在我的项目中为我的所有Scala文件提供了很多"Java Problem"条目.如何将Scala特性仅添加到src/test/scala?
干杯
聂
我在我的应用程序中设置了这样的身份验证,当提供用户名并且API密钥为123时总是允许:
object Auth {
def IsAuthenticated(block: => String => Request[AnyContent] => Result) = {
Security.Authenticated(RetrieveUser, HandleUnauthorized) { user =>
Action { request =>
block(user)(request)
}
}
}
def RetrieveUser(request: RequestHeader) = {
val auth = new String(base64Decode(request.headers.get("AUTHORIZATION").get.replaceFirst("Basic", "")))
val split = auth.split(":")
val user = split(0)
val pass = split(1)
Option(user)
}
def HandleUnauthorized(request: RequestHeader) = {
Results.Forbidden
}
def APIKey(apiKey: String)(f: => String => Request[AnyContent] => Result) = IsAuthenticated { user => request =>
if(apiKey == "123")
f(user)(request)
else …Run Code Online (Sandbox Code Playgroud) 我正在玩realm.io.我写了几个对象,现在我想查询它们.我的数据类:
class Sample : RLMObject {
dynamic var sampleKey : String = ""
}
Run Code Online (Sandbox Code Playgroud)
和我的查询代码
@IBAction func readLocalRecord(sender: UIButton) {
let s : NSString = NSString.stringWithString("sampleKey == SampleValue")
let p : NSPredicate = NSPredicate(format: "sampleKey = %@", argumentArray: NSArray(object: NSString.stringWithString("SampleValue")))
// the following throws exception, that I cannot catch in Swift:
// 'Unsupported predicate value type', reason: 'Object type any not supported'
let r = Sample.objectsWithPredicate(p)
}
Run Code Online (Sandbox Code Playgroud)
网站和RLMObject的标题表明我应该可以说Sample.objectsWhere("sampleKey ='SampleValue'")(或者类似的),但是对象给出了编译错误,抱怨函数不在那里,并且有没有自动完成功能.所以我尝试使用objectsForPredicate,但是这表示类型为'any'(挖掘标题,我发现这等于ObmC在Realm术语中的'id'类型).我在这做错了什么?我试着显得非常明确,确保使用NSString而不是String和NSArray而不是Array,但仍有些东西被解释为'id'而不是spesific类型.
有什么建议?
干杯
-Nik
我想编写我的UI测试,以便测试动态类型的所有七种状态,从最小到最大.我该怎么做?
我可以在我的Scheme中为模拟器设置环境变量,然后制作不同的方案吗?
或者我可以在我的测试中以编程方式设置动态类型变量吗?
我宁愿不做一个DynamicTypeController,然后让它说出它是什么类型,因为我冒险忘记将它用于某些元素,然后没有正确测试的行为.
干杯
聂
我创建了一个SnapOperationQueue,它是一个操作队列,有一些我喜欢的扩展.其中之一是对操作组进行重新优先排序.添加了一个操作,其中包含以下四个优先级之一(SnapOperationQueuePriority):
case Highest
case High
case Normal
case Low
Run Code Online (Sandbox Code Playgroud)
在当前的实现中,我有它.Low和.Highest不会改变,但.High和.Normal会.
我想改变这一点,以便我可以在优先级上设置上限和下限.也就是说,我可以说我们现在做的这个操作(.低)可能会变得非常重要(.High),即当预先缓存图像时,屏幕上突然需要该图像.这些阈值也应该能够说某些操作,即使在重新优先级的组中,也不会重新优先级低于某些操作,即应保留登录操作(.Highest).最高
为此,我想修改我的枚举,如下所示:
case Highest(lowerThreshold: SnapOperationQueuePriority = .Highest)
case High(lowerThreshold: SnapOperationQueuePriority = .Low, higherThreshold: SnapOperationQueuePriority = .High)
case Normal(lowerThreshold: SnapOperationQueuePriority = .Low, higherThreshold: SnapOperationQueuePriority = .High)
case Low(higherThreshold: SnapOperationQueuePriority = .Low)
Run Code Online (Sandbox Code Playgroud)
但是,我得到"在一个类型中不允许默认参数".我想在此处拥有默认参数的原因是,此更改不会破坏或更改已使用现有实现的任何代码的行为.
您是否可以建议我可以获得新优先级但不更改依赖于当前API的代码的API?
我有一个带有视图模型的视图,该视图中的操作可以更改视图模型。为了能够将逻辑分解为可重用的部分,我将视图的一部分作为它自己的视图,并带有一个 @Binding 到它需要的值。现在,我希望能够根据值的变化来执行一些逻辑,而不仅仅是视图的变化。我怎样才能做到这一点?如果它是一个常规属性,我会实现一个 didSet,但这让我无处可去。我想使用 Combine 并将 @Binding 视为发布者,但我也找不到办法做到这一点。建议?
这是代码:
class ViewModel: ObservableObject {
@Published var counter: Int = 0
}
struct Greeter: View {
@Binding var counter: Int {
didSet {
// this isn't printed....
print("Did set -> \(counter)")
}
}
init(counter: Binding<Int>) {
self._counter = counter
// ...so how about setting up a subscription to the @Binding counter above here?
}
var body: some View {
Text("Hello, world #\(counter)!")
.padding()
}
}
struct ContentView: View {
@ObservedObject var viewModel: …Run Code Online (Sandbox Code Playgroud) 我想制作一个在每一帧上更新的 Swift 图表 - 我希望监视不断更新的数据源。我制作了一个小演示视图来执行此操作,但 CPU 使用率非常高 - 恒定为 90%
我该怎么做才能更好地使用 SwiftUI,并将 CPU 使用率降低到更适度的水平,同时仍以屏幕刷新率进行更新?
我的例子在这里:
import SwiftUI
import Charts
struct DataPoint: Identifiable {
var id: Int { return x }
let x: Int
let y: Float
}
class ViewModel: ObservableObject {
@Published var data: [DataPoint] = []
init() {
data = _regen()
Task {
await loop()
}
}
func regen() {
data = _regen()
}
func loop() async {
try? await Task.sleep(nanoseconds: 1_000_000_000 / 120)
Task { @MainActor in
regen() …Run Code Online (Sandbox Code Playgroud) 我是Play的新手!&Scala,但我正在尝试创建一个服务,将JSON请求映射到Map [String,JsObject](或Map [String,JsValue],我不确定区别),然后输出一个列表密钥递归地通过地图(最好是树).
但我有开始的问题:
def genericJSONResponse = Action(parse.json) {
request => request.body
var keys = request.keys
Ok("OK")
}
Run Code Online (Sandbox Code Playgroud)
我期望这里的密钥是从请求中填充密钥,但当然,它不会编译.鉴于上述说明,我该如何处理?
在此先感谢您帮助Scala noob :-)
聂