GVKun编程网logo

持久性@Column nullable = false可以插入null(持久变量)

18

如果您对持久性@Columnnullable=false可以插入null和持久变量感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解持久性@Columnnullable=false可以插入null

如果您对持久性@Column nullable = false可以插入null持久变量感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解持久性@Column nullable = false可以插入null的各种细节,并对持久变量进行深入的分析,此外还有关于@ManyToOne(optional = false)与@Column(nullable = false)有什么区别、ALTER TABLE,在null null列中设置null,PostgreSQL 9.1、c# – String是Nullable返回false、c# – 与IsNullable冲突的配置设置= true IsNullable = false重用ComplexType的实用技巧。

本文目录一览:

持久性@Column nullable = false可以插入null(持久变量)

持久性@Column nullable = false可以插入null(持久变量)

我想做这个列不能为null,但是当我在数据库中插入一个寄存器值null时,这允许我插入。我阅读了文档,但我不知道为什么不起作用。
@Column(name="QWECOD", nullable = false) private String qwe;

谢谢

更新:我正在使用 Toplink 和java org.eclipse.persistence.eclipselink:2.4.2

答案1

小编典典

我认为如果您使用EntityManager的实现生成模式,则会使用可为空的。我不知道是否也必须在持久化实体时对其进行验证。

如果使用@NotNull批注可能会有所帮助,但这不是普通的JPA。它在JSR-303中定义


编辑:在JPA 2.1规范中,有此部分:

11.2.2.1列
在架构生成中使用了列注释的以下元素:
名称
唯一的
可为空的 列定义
表的
长度(仅字符串值的列)精度(仅精确的数字(十进制/数字)列)规模(精确的数字(十进制/数字)
)(仅限列))有关适用于这些元素和列创建的规则,请参见11.1.9节。AttributeOverride批注可用于覆盖列映射。

因为没有给出其他提示,所以我假设以下内容:如果遵循JPA的EntityManager创建架构,则它必须通过使用与数据库等效的约束(例如notnull)在特定列上应用可为空的约束。
,则Entitymanager BUT不会通过基础数据库对其进行检查。因此,如果数据库引发错误,则EntityManager将此错误传播到调用者。

如果您在不使用DB可为空的约束的情况下自行创建表,则entitymanager会尝试保留该实体,并且不会出现错误->持久化还可以,但是有些空值不应该存在。

@ManyToOne(optional = false)与@Column(nullable = false)有什么区别

@ManyToOne(optional = false)与@Column(nullable = false)有什么区别

在JPA中,我什么时候使用属性optional=false和注释感到困惑@Column(nullable=false)。有什么区别?

答案1

小编典典

@Column(nullable=false)是用于生成架构的指令。在类中生成的数据库列将在实际数据库中标记为不可为空。

optional=false是运行时指令。它所做的主要功能是与延迟加载有关。您不能延迟加载非集合映射的实体,除非您记得设置了optional =
false(因为Hibernate不知道那里应该有一个代理还是一个null,除非您告诉它null是不可能的,否则它可以生成代理人。)

ALTER TABLE,在null null列中设置null,PostgreSQL 9.1

ALTER TABLE,在null null列中设置null,PostgreSQL 9.1

我有一个表与非空列,如何设置一个null值在这一列作为默认值?

我的意思是,我想做这样的事情:

postgres=# ALTER TABLE person ALTER COLUMN phone SET NULL;

但它显示:

postgres=# ALTER TABLE person ALTER COLUMN phone SET NULL;
ERROR:  Syntax error at or near "NULL"
LINE 1: ALTER TABLE person ALTER COLUMN phone SET NULL;
ALTER TABLE person ALTER COLUMN phone DROP NOT NULL;

更多细节在手册:http://www.postgresql.org/docs/9.1/static/sql-altertable.html

c# – String是Nullable返回false

c# – String是Nullable返回false

为什么:
string s = "";
 bool sCanBeNull = (s is Nullable);
 s = null;

sCanBeNull等于假吗?

我正在编写代码生成器,并且需要确保传递给它的每个类型都可以为空(如果还没有).

//Get the underlying type:
       var type = field.FieldValueType;

        //Now make sure type is nullable:
        if (type.IsValueType) 
        {
            var nullableType = typeof (Nullable<>).MakeGenericType(type);
            return nullableType.FullName;
        }
        else
        {
            return type.FullName;
        }

我是否需要明确检查字符串或我遗失了什么?

解决方法

告诉您某个值是特定类型还是从该特定类型派生的值.

Nullable是一个通用结构,允许可空值的非可空值.

string不是Nullable

要判断某个类型是否具有空值,请使用以下事实:对于所有此类类型,默认值为null,而对于所有其他类型,它不是:

default(string) == null; // true

c# – 与IsNullable冲突的配置设置= true IsNullable = false重用ComplexType

c# – 与IsNullable冲突的配置设置= true IsNullable = false重用ComplexType

我不知道这种行为是设计的还是EF6中的错误,或者还有另一种方法可以做到这一点.有这种复杂的类型:

[ComplexType]
public partial class Company    
    public bool HasValue { get { return !string.IsNullOrEmpty(this.Name); } }

    [MaxLength(100)]
    public string Name { get; set; }

    [MaxLength(20)]
    public string PhoneNumber { get; set; }

    [MaxLength(128)]
    public string EmailAddress { get; set; }
}

我在这两个实体中重用它:

public partial class Customer
{
    public Customer ()
    {
        this.Company = new Company();       
    }

    [Key]
    public int IdCustomer { get; set; }

    [MaxLength(100)]
    [required]
    public string FirstName { get; set; }

    [MaxLength(100)]
    [required]
    public string LastName { get; set; }

    public Company Company { get; set; }

    public virtual AcademicInfo AcademicInfo { get; set; }
}


public partial class AcademicInfo
{       
    public AcademicInfo()
    {
        this.Organization = new Company();
    }       

    [Key,ForeignKey("Customer")]
    public int IdCustomer { get; set; }

    public Company Organization { get; set; }

    [MaxLength(100)]
    public string Subject { get; set; }

    [MaxLength(100)]
    public string Degree { get; set; }

    public virtual Customer Customer { get; set; }
}

在dbcontext的OnModelCreating中(编辑:为了简单起见,我添加了之前省略的FK代码):

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{    
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    // ... Other code here related to entities not related to the problem reported omitted to avoid confusion.

    modelBuilder.Entity<AcademicInfo>()
            .Hasrequired(a => a.Customer)
            .WithOptional(c => c.AcademicInfo)
            .WillCascadeOnDelete(true);

    modelBuilder.Entity<Customer>()
            .Property(p => p.Company.Name)
            .HasColumnName("CompanyName")
            .IsOptional(); // CONFLICT HERE
    modelBuilder.Entity<Customer>()
            .Property(p => p.Company.EmailAddress)
            .HasColumnName("CompanyEmailAddress")
            .IsOptional();  //CONFLICT HERE
    modelBuilder.Entity<Customer>()
            .Property(p => p.Company.PhoneNumber)
            .HasColumnName("CompanyPhoneNumber")
            .IsOptional();

    modelBuilder.Entity<AcademicInfo>()
            .Property(a => a.Organization.Name)
            .HasColumnName("OrganizationName")
            .Isrequired(); // CONFLICT
    modelBuilder.Entity<AcademicInfo>()
            .Property(a => a.Organization.EmailAddress)
            .HasColumnName("OrganizationEmail")
            .Isrequired(); // CONFLICT
    modelBuilder.Entity<AcademicInfo>()
            .Property(a => a.Organization.PhoneNumber)
            .HasColumnName("OrganizationPhone")
            .IsOptional();
}

Add-Migration命令失败,并显示以下错误:
为’Company’类型的属性’Name’指定了冲突的配置设置:
    IsNullable =与IsNullable = True的错误冲突

但它没有任何意义,因为我在AcademicInfo表中定义了不可为空的字段,在Customer表中定义了可为空的字段.

解决方法

这是一个老问题,但仍然适用于EF版本6.1.3.

根据this issue,行为是关于如何配置特定复杂类型的实体框架限制.

This is a limitation of EF,some property facets need to be stored in C-Space and EF doesn’t have a way of configuring a particular usage of a complex type. So you can only specify different S-Space facets like ColumnName or ColumnType

关于持久性@Column nullable = false可以插入null持久变量的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于@ManyToOne(optional = false)与@Column(nullable = false)有什么区别、ALTER TABLE,在null null列中设置null,PostgreSQL 9.1、c# – String是Nullable返回false、c# – 与IsNullable冲突的配置设置= true IsNullable = false重用ComplexType的相关信息,请在本站寻找。

本文标签: