我们在批处理作业中获得了ORA-00001(违反了唯一约束).但是,发出COMMIT时会发生错误,而不是在插入违规记录时发生.
问题:
任何帮助表示赞赏!
附加信息/问题:
"违规"约束标记为"立即"和"不可延迟".这可以在交易中被覆盖吗?
我创建了以下实体bean,并将两列指定为唯一.现在我的问题是创建的表没有唯一约束,并且日志中没有错误.有没有人有想法?
@Entity
@Table(name = "cm_blockList", uniqueConstraints = @UniqueConstraint(columnNames = {"terminal", "blockType"}))
public class BlockList {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name="terminal")
private Terminal terminal;
@Enumerated(EnumType.STRING)
private BlockType blockType;
private String regEx;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Play的CRUD模块来创建一组简单的管理屏幕.我的一个模型是用户,我想在电子邮件字段上强制执行唯一约束.
代码如下所示:
public class User extends Model {
@Email
@Required
@Column(unique=true)
public String email;
Run Code Online (Sandbox Code Playgroud)
管理屏幕正确显示 - 当我尝试打破唯一性时(通过使用已使用过的电子邮件保存用户)我收到此错误(在浏览器中):
Execution exception
PersistenceException occured : org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
In {module:crud}/app/controllers/CRUD.java (around line 100)
96:
} catch (TemplateNotFoundException e) {
97:
render("CRUD/show.html", type, object);
98:
}
99:
}
100:
<b>object._save();</b>
101:
flash.success(Messages.get("crud.saved", type.modelName));
102:
if (params.get("_save") != null) {
103:
redirect(request.controller + ".list");
104:
}
105:
redirect(request.controller + ".show", object._key());
106:
}
Run Code Online (Sandbox Code Playgroud)
我可以使用CRUD模块和列唯一性注释进行任何调整吗?
我遇到了DataAccessException处理的麻烦.
当违反了uniquess键约束时,我得到了一个JpaSystemException,而不是DuplicateKeyException!
我找到了一些线索谈论这个问题,但没有人帮我解决问题.如何映射到具体的org.springframework.dao异常?
我的persistence.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>...</class>
<class>...</class>
...
<exclude-unlisted-classes />
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<property name="hibernate.show.sql" value="true"/>
<property name="hibernate.ejb.event.post-insert" value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-update" value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-delete" value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.pre-collection-update" value="org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.pre-collection-remove" value="org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-collection-recreate" value="org.hibernate.envers.event.AuditEventListener" />
</properties>
</persistence-unit>
<persistence-unit name="persistenceUnitI24" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>...</class>
<class>...</class>
<exclude-unlisted-classes />
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/> …Run Code Online (Sandbox Code Playgroud) 几天后我对Doctrine实体有一点问题.我在两个字段上使用UniqueConstraint定义了它,之后在这两个字段上进行了UniqueEntity验证.但是在通过添加已经在base中的实体来验证我的表单之后,无法在我的表单中获取我的错误消息.
只是来自Symfony的错误消息:
SQLSTATE [23000]:完整性约束违规:1062重复条目'339057986-00012'用于键'SIRET'
这是我的实体声明,对我来说似乎都很好,但也许我忘了或误解了什么?
<?php
namespace Proetco\FrontBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Proetco\FrontBundle\Entity\Entreprise
*
* @ORM\Table(name="entreprise", uniqueConstraints={@ORM\UniqueConstraint(name="SIRET", columns={"SIREN", "NIC"})})
* @ORM\Entity(repositoryClass="Proetco\FrontBundle\Entity\EntrepriseRepository")
* @UniqueEntity(fields={"SIREN","NIC"}, message="Cette entreprise est déjà enregistrée")
*/
class Entreprise {
protected $Siret;
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $SIREN
*
* @ORM\Column(name="SIREN", type="string", length=9)
*/
private $SIREN;
/**
* @var string $NIC
*
* @ORM\Column(name="NIC", type="string", …Run Code Online (Sandbox Code Playgroud) 我公司开发的形式,即用户将他/她first name和last name.
对于username(一个unique属性),我设计了以下方法:
FirstName:harrY LastName:PottEr - > username:Harry-Potter
FirstName:HARRY LastName:POTTER - > username:Harry-Potter-1
FirstName:harrY LastName:PottEr - > username:Harry-Potter-2
等等..
这是我的函数定义:
def return_slug(firstname, lastname):
u_username = firstname.title()+'-'+lastname.title() //Step 1
u_username = '-'.join(u_username.split()) //Step 2
count = User.objects.filter(username=u_username).count() //Step 3
if count==0:
return (u_username)
else:
return (u_username+'-%s' % count)
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚Step 3在实施之前该做些什么.我应该在哪里[:len(u_username)]比较字符串?
编辑:
如果存在多个实例Harry-Potter,则通过解决最后添加整数的问题来应用此方法.我的问题是:我如何检查附加的最后一个整数是什么Harry-Potter.
假设我在表中有3列--A,B和C.我想确保如果我在列A中插入一些值(比如说x),我就不能插入一个B或C等于x的元组,即,对于所有元组,值x应对于列A保持唯一.
注意,对于其他元组,可以在A中重复x.
我知道SQL中的UNIQUE子句,但这只是为了确保一个值只在特定列中出现一次.由于Oracle中的CHECK语句不允许子查询,我无法弄清楚如何实现它.
编辑(添加更多信息)
主键是Employee_Number,而有问题的3列是LandlineNo,MobileNo和VOIP.因此,假设这是一个条目:
Employee_Number = 1, LandlineNo = x, MobileNo = y, VOIP = z
Run Code Online (Sandbox Code Playgroud)
然后不允许这个另一个元组的条目 -
Employee_Number = 2, LandlineNo = a, MobileNo = x, VOIP = c
Run Code Online (Sandbox Code Playgroud)
另一方面,这个很好(是的,2名员工可以拥有相同类型的相同数量)
Employee_Number = 2, LandlineNo = x, MobileNo = b, VOIP = c
Run Code Online (Sandbox Code Playgroud) 给出下表(和示例数据):
PK | ClientID | SetID | Title
-----------------------------
P1 | C1 | S1 | Title1
P2 | C1 | S1 | Title1
P3 | C2 | S2 | Title1
P4 | C2 | S2 | Title1
P3 | C1 | S3 | Title2
P5 | C1 | S3 | Title2
Run Code Online (Sandbox Code Playgroud)
假设a Set属于a Client,我是否可以使用一个唯一的索引来限制标题在客户端中是唯一的,除非它在同一个集合中有兄弟姐妹.
例如,我可以有Title1两个Clients而不是两个一个Client.现在Client1,我希望有第二个记录,Title1但只有当它与SetID其他所有记录相同时Title.
需要注意的是,我正在使用SQL Azure,但我也更感兴趣(例如2008 R2/2012).
编辑:
请注意我无法更改表格的结构.它已经以这种方式存在,并且背后有一个复杂的业务层.如果我可以解决这个问题,那么很好,如果没有,那么我可以让它破碎.
MS SQL Server不会忽略空值并将其视为UNIQUE KEY约束的违规,但我所知道的是UNIQUE KEY与接受空值的主键不同.
Violation of UNIQUE KEY constraint 'AK_UserName'. Cannot insert duplicate key in object 'dbo.users'. The duplicate key value is (<NULL>).
The statement has been terminated.
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我解决这个问题吗?
对于以下 API 端点,
import json
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse, HttpResponseBadRequest
from django.views.decorators.http import require_POST
from lucy_web.models import UserApn
@login_required
@require_POST
def save_apn(request, version):
player_id = json.loads(request.body).get('player_id')
if player_id:
UserApn.objects.get_or_create(user=request.user, player_id=player_id)
return JsonResponse({'status': 'success'})
else:
return HttpResponseBadRequest()
Run Code Online (Sandbox Code Playgroud)
这是底层模型:
from django.contrib.auth.models import User
from django.db import models
from .timestamped_model import TimeStampedModel
class UserApn(TimeStampedModel):
user = models.ForeignKey(User)
player_id = models.CharField(max_length=255)
Run Code Online (Sandbox Code Playgroud)
对 的调用get_or_create()引发了一些MultipleObjectsReturned错误。为了解决这个问题,我想对和施加unique_together约束。然而,首先,我必须编写一个数据迁移,以消除违反此唯一共同约束的行。userplayer_id
我如何编写一个选择这些的查询?目前已提出以下建议:
def remove_duplicate_apns(apps, schema_editor):
UserApn = apps.get_model('lucy_web', 'UserApn')
previous_user_id …Run Code Online (Sandbox Code Playgroud)