我想知道在Doctrine2中使用多对多关系的最好,最干净,最简单的方法是什么.
让我们假设我们有一张专辑,如Metallica的Master of Puppets,有几首曲目.但请注意,一首曲目可能会出现在一张专辑中,比如Metal by Metallica的专辑 - 三张专辑都是这首曲目.
所以我需要的是专辑和曲目之间的多对多关系,使用第三个表和一些额外的列(比如指定专辑中曲目的位置).实际上,我必须使用,如Doctrine的文档所示,实现该功能的双重一对多关系.
/** @Entity() */
class Album {
/** @Id @Column(type="integer") */
protected $id;
/** @Column() */
protected $title;
/** @OneToMany(targetEntity="AlbumTrackReference", mappedBy="album") */
protected $tracklist;
public function __construct() {
$this->tracklist = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getTitle() {
return $this->title;
}
public function getTracklist() {
return $this->tracklist->toArray();
}
}
/** @Entity() */
class Track {
/** @Id @Column(type="integer") */
protected $id;
/** @Column() */
protected $title;
/** …Run Code Online (Sandbox Code Playgroud) 我有一个文本文件包含1 000 002以下编号中的数字:
123 456
1 2 3 4 5 6 .... 999999 100000
Run Code Online (Sandbox Code Playgroud)
现在我需要读取该数据并将其分配给int变量(前两个数字)和其余所有(1 000 000个数字)到一个数组int[].
这不是一项艰巨的任务,但是 - 它的速度非常慢.
java.util.Scanner: Scanner stdin = new Scanner(new File("./path"));
int n = stdin.nextInt();
int t = stdin.nextInt();
int array[] = new array[n];
for (int i = 0; i < n; i++) {
array[i] = stdin.nextInt();
}
Run Code Online (Sandbox Code Playgroud)
它作为例外工作,但执行大约需要7500毫秒.我需要在几百毫秒内获取该数据.
java.io.BufferedReader:使用BufferedReader.readLine()和String.split()我在大约1700毫秒内得到了相同的结果,但它仍然太多了.
如何在不到1秒的时间内读取该数据量?最终结果应该等于:
int n …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Jackson 2.0-RC3读取遗留的JSON代码,但是我坚持使用"嵌入式"对象.
给出以下JSON:
{
"title": "Hello world!",
"date": "2012-02-02 12:23:34".
"author": "username",
"author_avatar": "http://.../",
"author_group": 123,
"author_prop": "value"
}
Run Code Online (Sandbox Code Playgroud)
如何将其映射到以下结构中:
class Author {
@JsonPropery("author")
private String name;
@JsonPropery("author_avatar")
private URL avatar;
@JsonProperty("author_group")
private Integer group;
...
}
class Item {
private String title;
@JsonProperty("date")
private Date createdAt;
// How to map this?
private Author author;
}
Run Code Online (Sandbox Code Playgroud)
我试图这样做,@JsonDeserialize但似乎我必须以Item这种方式映射整个对象.
有没有办法在persist/flush之前获取实体ID?我的意思是:
$entity = new PointData();
$form = $this->createForm(new PointDataType(), $entity);
Run Code Online (Sandbox Code Playgroud)
如果我此时尝试$ entity-> getId(),它什么都不返回.
我可以通过以下方式获得它:
$em->persist($entity);
$em->flush();
Run Code Online (Sandbox Code Playgroud)
(假设$ em = $ this-> getDoctrine() - > getEntityManager();)
我怎样才能做到这一点?
我正在使用IIS上的ASP.NET 4.6,C#,OWIN管道Microsoft.Owin.Host.SystemWeb,大量异步方法调用和标准的全局资源文件(*.resxin App_GlobalResources)创建一个简单的多语言网站.该网站使用MVC5,WebAPI2和Autofac作为依赖性解析器.
我无法正确更改生成的页面的区域设置/文化,因为异步方法每个请求使用多个线程,我找不到Thread.Current[UI]Culture为给定请求关联的每个线程设置的方法,因为这些属性未同步.我还想保持干净的代码,而没有"异步/等待文化配置"搞乱有用的代码.
Startup.cs
public void Configuration(IAppBuilder app)
{
...
app.UseAutofacMiddleware(container);
app.UseAutofacMvc();
app.UseAutofacWebApi(httpConfiguration);
app.UseWebApi(httpConfiguration);
...
app.Use(async (ctx, next) =>
{
/* in production, detect based on current URL and/or cookie */
var culture = new CultureInfo("pl_PL");
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = culture;
await next.Invoke();
});
}
Run Code Online (Sandbox Code Playgroud)
SampleController.cs
public async Task<ActionResult> SayHello()
{
// returns pl_PL message
var msgA = Resources.Messages.HelloWorld;
await someService.doSthAsync();
// returns system default (en_US) message
var msgB …Run Code Online (Sandbox Code Playgroud) class Singer
{
String name;
String album;
public Singer(){
name="Whitney Houson";
album="Latest Releases";
}
public static void main(String[] args)
{
System.out.println("Name of the singer is "+name);
System.out.println("Album Information stored for "+album);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我发现错误,表示无法从静态上下文引用非静态变量名称
为了简单起见:
我有一个简单的UserJS脚本(FF/Chrome:Greasemonkey,Opera:内置),修复了我经常使用的网站上的一些问题.问题是我需要在不同的请求之间存储一些数据.我的第一次尝试LocalStorage,但是,当它来到与子域的工作就失败:www.domain.com和subdomain.domain.com(不幸的是根域是子域-事实上愚蠢www).我需要能够访问一些可在任何地方使用的数据源.
现在我被困 - 任何想法?
我刚刚开始发现Doctrine并注意到官方文档指向1.2,因为这似乎是当前的稳定版本.
我也注意到很多人似乎都提到Doctrine 2与Doctrine 1相比是一个"差异世界",但是在查看What's new页面时,我并没有真正看到广泛的更改日志.我猜这个列表只显示了与之前2.x版本相比的最新增加内容.在谷歌搜索同样的问题之后,我期待找到一个很好的差异综合报道,但我似乎无法理解.
我目前正在阅读Doctrine 1手册,并且一读完就会对Doctrine 2做同样的事情,但是对这些重大变化的正确概述将非常感激,所以我的问题是:
Doctrine1和Doctrine2有什么区别?
拥有以下相当简单的代码和正确配置的基于JTA的持久化上下文:
abstract class AbstractRepository<E> {
@PersistenceContext
protected EntityManager em;
@Transactional
public synchronized void persist(E entity) {
em.persist(entity);
em.flush();
}
}
@ApplicationScoped
class MyEntityRepository extends AbstractRepository<MyEntity> {
}
Run Code Online (Sandbox Code Playgroud)
我在调用时遇到以下异常MyEntityRepository.persist():
2015-06-23T12:34:55.233+0200|Severe: javax.persistence.TransactionRequiredException
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTxRequiredCheck(EntityManagerWrapper.java:161)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTransactionScopedTxCheck(EntityManagerWrapper.java:151)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:281)
at my.project.AbstractRepository.persist(AbstractRepository.java:28)
at my.project.QuestionnaireRepository.persist(QuestionnaireRepository.java:1)
at my.project.QuestionnaireRepository$Proxy$_$$_WeldClientProxy.persist(Unknown Source)
at my.project.QuestionnaireForm.save(QuestionnaireForm.java:29)
at my.project.QuestionnaireForm.lambda$0(QuestionnaireForm.java:1)
at my.project.QuestionnaireForm$$Lambda$56/1079229220.buttonClick(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:977)
at com.vaadin.ui.Button.fireClick(Button.java:393)
at com.vaadin.ui.Button$1.click(Button.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) …Run Code Online (Sandbox Code Playgroud) 我有以下两个控制器:
[RoutePrefix("/some-resources")
class CreationController : ApiController
{
[HttpPost, Route]
public ... CreateResource(CreateData input)
{
// ...
}
}
[RoutePrefix("/some-resources")
class DisplayController : ApiController
{
[HttpGet, Route]
public ... ListAllResources()
{
// ...
}
[HttpGet, Route("{publicKey:guid}"]
public ... ShowSingleResource(Guid publicKey)
{
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
所有这三个行动实际上都有三条不同的路线:
GET /some-resourcesPOST /some-resourcesGET /some-resources/aaaaa-bbb-ccc-dddd如果我将它们放入单个控制器中,一切正常,但是如果我将它们分开(如上所示),WebApi会抛出以下异常:
找到了与URL匹配的多种控制器类型.如果多个控制器上的属性路由与请求的URL匹配,则会发生这种情
这个消息很明显.在寻找适合控制器/操作的候选者时,WebApi似乎没有考虑HTTP方法.
我怎样才能实现预期的行为?
更新:我已经深入研究了Web API内部,我明白这是默认情况下的工作方式.我的目标是分离代码和逻辑 - 在现实世界中,这些控制器具有不同的依赖关系并且更复杂一些.为了维护,可测试性,项目组织等,它们应该是不同的对象(SOLID和东西).
我认为我可以覆盖一些WebAPI服务(IControllerSelector等),但这似乎是一个有点风险和非标准的方法,这个简单的 - 和我假设 - 常见的情况.
c# asp.net-web-api attributerouting asp.net-web-api-routing asp.net-web-api2
java ×4
doctrine-orm ×3
c# ×2
doctrine ×2
orm ×2
php ×2
async-await ×1
asynchronous ×1
cdi ×1
entity ×1
flush ×1
input ×1
jackson ×1
javascript ×1
json ×1
mapping ×1
model ×1
optimization ×1
owin ×1
performance ×1
persist ×1
subdomain ×1
transactions ×1