I have a base entity and 3 inherited sub-entities, called NoteItem, ImageItem and MapItem. The base entity class is marked with the [InheritanceMapping] attribute and has a Discriminator column. Everything works fine, except the third MapItem entity, which contains 4 columns of type double and bool.
For example, if I insert the a NoteItem entity it will throw an exception:
SqlCeException - Column Pitch can not contain NULL values
Pitch is column with double type. Only if I extend the [Column] attribute of each double and bool column with the CanBeNull = true addition, then it works.
It seems to me that there's a problem with double and bool values, because a column with type string must not explicitly contain the CanBeNull addition.
Is this a known problem or am I wrong?
there's a problem with double and bool values
AFAIK the behavior is by design.
a column with type string must not explicitly contain the CanBeNull addition
A string CLR value can be null, thus without [Column( CanBeNull = false )] attribute specified, the entity framework generates column that can be null. OTOH, bool and double values can't be null, if you want to allow nulls, you must specify [Column( CanBeNull = true )]
If you don't want to decorate your double and bool properties/fields with [Column] attributes, you could e.g. define them as double? and bool?, respectively.
Related
I have mentioned two fields(Name, Age) in my golang Struct after few days I have added one more fields in my database(Name,Age, Salary ) not in golandg struct.It shows errors like(Error 1364: Field 'salary' doesn't have a default value). How to ignore fileds in my struct dynamically
type Employee struct {
Name string `json:"name"
Age int `json:"age"
}
In future i will add more fileds but i don't want mention in struct
Based on the error you are getting, it seems like you are trying to insert a row into a MySQL database, and the new row you added does not have a default value. So your options are to :
Add a default value to the new column - See: https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html
Add a field for the column into your Go struct and supply a value for it.
In my model I have fields like this:
HidePricingFromProfile bool `json:"hidePricingFromProfile"`
When I update the model in DB:
store.storage.DB.
Where("artist_account_id = ?", m.ArtistAccountID).
Assign(model).
FirstOrCreate(m).
Error
It updates boolean fields only to true values.
this field is defined in DB like this:
hide_pricing_from_profile boolean default false not null
From doc
NOTE all fields having a zero value, like 0, '', false or other zero
values, won’t be saved into the database but will use its default
value
The zero value of bool is false that's why it not updated. You need to use pointer of bool which zero value is nil then false value will be updated in the database.
HidePricingFromProfile *bool `json:"hidePricingFromProfile"`
When I retrieve the entity object using JPA, it is getting "nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property". I have added property called nullable true also.
#Column(name="lat",nullable = true)
private double lat;
You can't have a nullable primitive type, that's a contradiction (in Java, at least).
If you want lat to be nullable, you need to use the Double java type, not double
I have a grails domain class that looks like:
class Person {
String name
int age
}
When I show the default "create" view (using scaffolding), the age field shows as a required field (with an asterisk next to it). Is there a way to make it show up as non-required and default to blank?
I've tried adding
constraints = {
age blank:true, nullable:true
}
This results in the field being allowed to be empty but it still shows up with the asterisk next to it.
An int is a primitive type and cannot be blank. You would have to change it to an Integer, then a null value would mean that it's blank.
I got a Entity with a Integer
#Entity(name=customer)
public Customer {
...
#Column
private int number;
...
//getter,setter
}
Now I want to cast this Integer in a query to compare it with an other value.
I tried this Query:
"SELECT c FROM customer c WHERE CAST(c.number AS TEXT) LIKE '1%'"
But it doesn't work.
This works in some of my code using Hibernate:
SELECT c FROM customer c WHERE STR(c.number) LIKE '1%'
In general, this is what the Hibernate docs (14.10 Expressions) say on casting:
str() for converting numeric or temporal values to a readable string
cast(... as ...), where the second argument is the name of a Hibernate
type, and extract(... from ...) if ANSI cast() and extract() is
supported by the underlying database
Since EJB3 EJBQL has been (almost) replaced by JPQL. In EJBQL and according to http://docs.oracle.com/cd/E11035_01/kodo41/full/html/ejb3_langref.html in JPQL as well there is no functionality to CAST a property of an entity.
So like I already told there are two options left:
Use a native Query.
Add special cast methods to the entities.
You need to specify the column you're selecting from table alias c, and since EJBQL doesn't support a cast function, pass a string into the query instead of text. (This effectively allows your program to do the cast before it gets to EJBQL.)
The example below is in SQL Server, but should give you the idea:
declare #numberText as varchar(50)
set #numberText = '1'
SELECT c.CustomerNumber FROM customer c
WHERE c.CustomerNumber LIKE #numbertext + '%'
So instead of private int number use private string numberText.
NOTE: I edited this answer after OP confirmed EJBQL does not support a CAST function.