JPA Annotation @Column(insertable=false) is being ignored, why?











up vote
2
down vote

favorite
1












I want one of the fields to be ignored when called save() method. The field is gonna get populated automatically by the database and returned. It should be treated as a read-only field.



I am concerned about private Timestamp ts; field:



@Entity
@Table(name = "time_series", schema = "ms")
@IdClass(Reading.class)
public class Reading implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "name", nullable = false)
private String sensorName;

@Id
@Column(name = "ts", insertable = false, updatable = false)
private Timestamp ts;

@Column(name = "reading")
private Double value;
...


As you see, I use insertable = false, updatable = false are inside the @Column annotation, so I'd expect that ts is ignored when forming the actual SQL behind the curtain.



@Override
@Transactional(readOnly = false)
public Reading save(Reading r) {
return readingRepository.save(r);
}


ReadingRepository is basically extended Spring's CrudRepository which has save(...) method.



When I save Reading object with ts=null I get an error from Postgres:




ERROR: null value in column "ts" violates not-null constraint




because Spring Data did not actually ignore the ts field based what I see from the log:



insert into ms.time_series (ts, name, reading) values (NULL, 'sensor1', 10.0)


Clearly, I want the query to be without ts like this:



insert into ms.time_series (name, reading) values ('sensor1', 10.0)


Why is the field not being ignored?



Now if you ask me whether my database schema is okay I say yes. When I type SQL query in console without the ts everything is fine. I even tried @Generated and @GeneratedValue annotations. Name and ts are both forming a primary key for the table, however, the result is the same if I make only one of them a PK or if I add an extra surrogate ID column. Same result...



Am I overlooking something or is there maybe a bug in the Spring framework?? I am using Spring 5.1.2 and SpringData 2.1.2



Note: If I use @Transient annotation that persists the insert query correctly but then the field is being ignored completely even on read/fetch.



Many thanks for any help with this!










share|improve this question
























  • use @Transient annotation on your Timestamp field.
    – Angad Bansode
    Nov 13 at 13:22










  • If it isn't insertable or updatable how can it be null? Also your mapping is weird how can the @IdClass be the same as the entity class?
    – M. Deinum
    Nov 13 at 14:04










  • @M.Deinum I had to add @IdClass so that I can have two @Ids here. As I mentioned, when I removed that the error still persisted.
    – user3732445
    Nov 13 at 19:47










  • I know what @IdClass is for, but it should be a separate class and not point to the entity. Also an @Id that isn't generated (you need to specify that) needs to be inserted. So basically your mapping is conflicting. You don't want a generated value, but don't want to insert it as. So hibernate needs to make a decisions.
    – M. Deinum
    Nov 13 at 19:50










  • @AngadBansode please, read my post, I cannot use @Transient because I need to be able to fetch and read that value
    – user3732445
    Nov 13 at 19:50















up vote
2
down vote

favorite
1












I want one of the fields to be ignored when called save() method. The field is gonna get populated automatically by the database and returned. It should be treated as a read-only field.



I am concerned about private Timestamp ts; field:



@Entity
@Table(name = "time_series", schema = "ms")
@IdClass(Reading.class)
public class Reading implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "name", nullable = false)
private String sensorName;

@Id
@Column(name = "ts", insertable = false, updatable = false)
private Timestamp ts;

@Column(name = "reading")
private Double value;
...


As you see, I use insertable = false, updatable = false are inside the @Column annotation, so I'd expect that ts is ignored when forming the actual SQL behind the curtain.



@Override
@Transactional(readOnly = false)
public Reading save(Reading r) {
return readingRepository.save(r);
}


ReadingRepository is basically extended Spring's CrudRepository which has save(...) method.



When I save Reading object with ts=null I get an error from Postgres:




ERROR: null value in column "ts" violates not-null constraint




because Spring Data did not actually ignore the ts field based what I see from the log:



insert into ms.time_series (ts, name, reading) values (NULL, 'sensor1', 10.0)


Clearly, I want the query to be without ts like this:



insert into ms.time_series (name, reading) values ('sensor1', 10.0)


Why is the field not being ignored?



Now if you ask me whether my database schema is okay I say yes. When I type SQL query in console without the ts everything is fine. I even tried @Generated and @GeneratedValue annotations. Name and ts are both forming a primary key for the table, however, the result is the same if I make only one of them a PK or if I add an extra surrogate ID column. Same result...



Am I overlooking something or is there maybe a bug in the Spring framework?? I am using Spring 5.1.2 and SpringData 2.1.2



Note: If I use @Transient annotation that persists the insert query correctly but then the field is being ignored completely even on read/fetch.



Many thanks for any help with this!










share|improve this question
























  • use @Transient annotation on your Timestamp field.
    – Angad Bansode
    Nov 13 at 13:22










  • If it isn't insertable or updatable how can it be null? Also your mapping is weird how can the @IdClass be the same as the entity class?
    – M. Deinum
    Nov 13 at 14:04










  • @M.Deinum I had to add @IdClass so that I can have two @Ids here. As I mentioned, when I removed that the error still persisted.
    – user3732445
    Nov 13 at 19:47










  • I know what @IdClass is for, but it should be a separate class and not point to the entity. Also an @Id that isn't generated (you need to specify that) needs to be inserted. So basically your mapping is conflicting. You don't want a generated value, but don't want to insert it as. So hibernate needs to make a decisions.
    – M. Deinum
    Nov 13 at 19:50










  • @AngadBansode please, read my post, I cannot use @Transient because I need to be able to fetch and read that value
    – user3732445
    Nov 13 at 19:50













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I want one of the fields to be ignored when called save() method. The field is gonna get populated automatically by the database and returned. It should be treated as a read-only field.



I am concerned about private Timestamp ts; field:



@Entity
@Table(name = "time_series", schema = "ms")
@IdClass(Reading.class)
public class Reading implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "name", nullable = false)
private String sensorName;

@Id
@Column(name = "ts", insertable = false, updatable = false)
private Timestamp ts;

@Column(name = "reading")
private Double value;
...


As you see, I use insertable = false, updatable = false are inside the @Column annotation, so I'd expect that ts is ignored when forming the actual SQL behind the curtain.



@Override
@Transactional(readOnly = false)
public Reading save(Reading r) {
return readingRepository.save(r);
}


ReadingRepository is basically extended Spring's CrudRepository which has save(...) method.



When I save Reading object with ts=null I get an error from Postgres:




ERROR: null value in column "ts" violates not-null constraint




because Spring Data did not actually ignore the ts field based what I see from the log:



insert into ms.time_series (ts, name, reading) values (NULL, 'sensor1', 10.0)


Clearly, I want the query to be without ts like this:



insert into ms.time_series (name, reading) values ('sensor1', 10.0)


Why is the field not being ignored?



Now if you ask me whether my database schema is okay I say yes. When I type SQL query in console without the ts everything is fine. I even tried @Generated and @GeneratedValue annotations. Name and ts are both forming a primary key for the table, however, the result is the same if I make only one of them a PK or if I add an extra surrogate ID column. Same result...



Am I overlooking something or is there maybe a bug in the Spring framework?? I am using Spring 5.1.2 and SpringData 2.1.2



Note: If I use @Transient annotation that persists the insert query correctly but then the field is being ignored completely even on read/fetch.



Many thanks for any help with this!










share|improve this question















I want one of the fields to be ignored when called save() method. The field is gonna get populated automatically by the database and returned. It should be treated as a read-only field.



I am concerned about private Timestamp ts; field:



@Entity
@Table(name = "time_series", schema = "ms")
@IdClass(Reading.class)
public class Reading implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "name", nullable = false)
private String sensorName;

@Id
@Column(name = "ts", insertable = false, updatable = false)
private Timestamp ts;

@Column(name = "reading")
private Double value;
...


As you see, I use insertable = false, updatable = false are inside the @Column annotation, so I'd expect that ts is ignored when forming the actual SQL behind the curtain.



@Override
@Transactional(readOnly = false)
public Reading save(Reading r) {
return readingRepository.save(r);
}


ReadingRepository is basically extended Spring's CrudRepository which has save(...) method.



When I save Reading object with ts=null I get an error from Postgres:




ERROR: null value in column "ts" violates not-null constraint




because Spring Data did not actually ignore the ts field based what I see from the log:



insert into ms.time_series (ts, name, reading) values (NULL, 'sensor1', 10.0)


Clearly, I want the query to be without ts like this:



insert into ms.time_series (name, reading) values ('sensor1', 10.0)


Why is the field not being ignored?



Now if you ask me whether my database schema is okay I say yes. When I type SQL query in console without the ts everything is fine. I even tried @Generated and @GeneratedValue annotations. Name and ts are both forming a primary key for the table, however, the result is the same if I make only one of them a PK or if I add an extra surrogate ID column. Same result...



Am I overlooking something or is there maybe a bug in the Spring framework?? I am using Spring 5.1.2 and SpringData 2.1.2



Note: If I use @Transient annotation that persists the insert query correctly but then the field is being ignored completely even on read/fetch.



Many thanks for any help with this!







java spring hibernate jpa spring-data-jpa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 9:41









barbsan

2,140922




2,140922










asked Nov 13 at 9:31









user3732445

112




112












  • use @Transient annotation on your Timestamp field.
    – Angad Bansode
    Nov 13 at 13:22










  • If it isn't insertable or updatable how can it be null? Also your mapping is weird how can the @IdClass be the same as the entity class?
    – M. Deinum
    Nov 13 at 14:04










  • @M.Deinum I had to add @IdClass so that I can have two @Ids here. As I mentioned, when I removed that the error still persisted.
    – user3732445
    Nov 13 at 19:47










  • I know what @IdClass is for, but it should be a separate class and not point to the entity. Also an @Id that isn't generated (you need to specify that) needs to be inserted. So basically your mapping is conflicting. You don't want a generated value, but don't want to insert it as. So hibernate needs to make a decisions.
    – M. Deinum
    Nov 13 at 19:50










  • @AngadBansode please, read my post, I cannot use @Transient because I need to be able to fetch and read that value
    – user3732445
    Nov 13 at 19:50


















  • use @Transient annotation on your Timestamp field.
    – Angad Bansode
    Nov 13 at 13:22










  • If it isn't insertable or updatable how can it be null? Also your mapping is weird how can the @IdClass be the same as the entity class?
    – M. Deinum
    Nov 13 at 14:04










  • @M.Deinum I had to add @IdClass so that I can have two @Ids here. As I mentioned, when I removed that the error still persisted.
    – user3732445
    Nov 13 at 19:47










  • I know what @IdClass is for, but it should be a separate class and not point to the entity. Also an @Id that isn't generated (you need to specify that) needs to be inserted. So basically your mapping is conflicting. You don't want a generated value, but don't want to insert it as. So hibernate needs to make a decisions.
    – M. Deinum
    Nov 13 at 19:50










  • @AngadBansode please, read my post, I cannot use @Transient because I need to be able to fetch and read that value
    – user3732445
    Nov 13 at 19:50
















use @Transient annotation on your Timestamp field.
– Angad Bansode
Nov 13 at 13:22




use @Transient annotation on your Timestamp field.
– Angad Bansode
Nov 13 at 13:22












If it isn't insertable or updatable how can it be null? Also your mapping is weird how can the @IdClass be the same as the entity class?
– M. Deinum
Nov 13 at 14:04




If it isn't insertable or updatable how can it be null? Also your mapping is weird how can the @IdClass be the same as the entity class?
– M. Deinum
Nov 13 at 14:04












@M.Deinum I had to add @IdClass so that I can have two @Ids here. As I mentioned, when I removed that the error still persisted.
– user3732445
Nov 13 at 19:47




@M.Deinum I had to add @IdClass so that I can have two @Ids here. As I mentioned, when I removed that the error still persisted.
– user3732445
Nov 13 at 19:47












I know what @IdClass is for, but it should be a separate class and not point to the entity. Also an @Id that isn't generated (you need to specify that) needs to be inserted. So basically your mapping is conflicting. You don't want a generated value, but don't want to insert it as. So hibernate needs to make a decisions.
– M. Deinum
Nov 13 at 19:50




I know what @IdClass is for, but it should be a separate class and not point to the entity. Also an @Id that isn't generated (you need to specify that) needs to be inserted. So basically your mapping is conflicting. You don't want a generated value, but don't want to insert it as. So hibernate needs to make a decisions.
– M. Deinum
Nov 13 at 19:50












@AngadBansode please, read my post, I cannot use @Transient because I need to be able to fetch and read that value
– user3732445
Nov 13 at 19:50




@AngadBansode please, read my post, I cannot use @Transient because I need to be able to fetch and read that value
– user3732445
Nov 13 at 19:50












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Try using GenericGenerator and GeneratedValue in your code.



Add the needed annotation and give values to all other members in Reading class, except ts.



Here some examples.






share|improve this answer





















  • Thank! I tried that and it didn't work, maybe I haven't come up with the right choice permutation yet.
    – user3732445
    Nov 13 at 9:57










  • Anyways, is it a good idea to mix javax.persistence.* org.hibernate.annotations.* annotations together?? GeneratedValue is javax and GenericGenerator is hibernate.
    – user3732445
    Nov 13 at 9:57


















up vote
0
down vote













As you say




I get an error from Postgres




If you check the docs it states:




Technically, a primary key constraint is simply a combination of a unique constraint and a not-null constraint.




That's also true for multi-column primary keys (see here)



So, if ts is part of your primary key in the database (as the @Id indicates) it's simply not possible to insert null values in that column.



IMO Hibernate/Spring got nothing to do with that as



insert into ms.time_series (ts, name, reading) values (NULL, 'sensor1', 10.0)


should be equivalent to



insert into ms.time_series (name, reading) values ('sensor1', 10.0)





share|improve this answer





















  • well if Hibernate/Spring have nothing to do with this, then the SQL query sent should be without the "ts" still. Also I said I tried to make the "ts" not part of the primary key and the error still persisted
    – user3732445
    Nov 13 at 19:43










  • CREATE TABLE ms.time_series ( ts TIMESTAMP(0) DEFAULT ('now'::text)::timestamp(0), name VARCHAR(255), reading DOUBLE PRECISION, PRIMARY KEY(ts, name) ); in SQL console I can run a query insert into ms.time_series (name, reading) values ('sensor1', 10) no problem
    – user3732445
    Nov 13 at 19:49










  • Why are you creating a timestamp with now, convert that to text to convert that to a timestamp again? What is the reasoning behind that.
    – M. Deinum
    Nov 13 at 19:50










  • @M.Deinum I copy pasted that from somewhere, I will fix it, but I don't believe it is the cause of the problem. The problem is that Spring/Hibernate is sending an incorrect SQL query which may be either caused by me improperly using the annotations or a bug in the framework itself.
    – user3732445
    Nov 13 at 19:57










  • It isn't an incorrect SQL, the SQL is perfectly valid. Spring has nothing to do with. The problem is the mapping. You want a non-generated ID and don't want to insert it. Hibernate has to make a decisions and decides that it needs to insert the currently assigned value. You need to tell hibernate that it should use a database generated value, else hibernate doesn't know how to handle it. It currently assumes client assigned/generated ids.
    – M. Deinum
    Nov 13 at 20:00











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277820%2fjpa-annotation-columninsertable-false-is-being-ignored-why%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Try using GenericGenerator and GeneratedValue in your code.



Add the needed annotation and give values to all other members in Reading class, except ts.



Here some examples.






share|improve this answer





















  • Thank! I tried that and it didn't work, maybe I haven't come up with the right choice permutation yet.
    – user3732445
    Nov 13 at 9:57










  • Anyways, is it a good idea to mix javax.persistence.* org.hibernate.annotations.* annotations together?? GeneratedValue is javax and GenericGenerator is hibernate.
    – user3732445
    Nov 13 at 9:57















up vote
0
down vote













Try using GenericGenerator and GeneratedValue in your code.



Add the needed annotation and give values to all other members in Reading class, except ts.



Here some examples.






share|improve this answer





















  • Thank! I tried that and it didn't work, maybe I haven't come up with the right choice permutation yet.
    – user3732445
    Nov 13 at 9:57










  • Anyways, is it a good idea to mix javax.persistence.* org.hibernate.annotations.* annotations together?? GeneratedValue is javax and GenericGenerator is hibernate.
    – user3732445
    Nov 13 at 9:57













up vote
0
down vote










up vote
0
down vote









Try using GenericGenerator and GeneratedValue in your code.



Add the needed annotation and give values to all other members in Reading class, except ts.



Here some examples.






share|improve this answer












Try using GenericGenerator and GeneratedValue in your code.



Add the needed annotation and give values to all other members in Reading class, except ts.



Here some examples.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 9:43









Bala555

355




355












  • Thank! I tried that and it didn't work, maybe I haven't come up with the right choice permutation yet.
    – user3732445
    Nov 13 at 9:57










  • Anyways, is it a good idea to mix javax.persistence.* org.hibernate.annotations.* annotations together?? GeneratedValue is javax and GenericGenerator is hibernate.
    – user3732445
    Nov 13 at 9:57


















  • Thank! I tried that and it didn't work, maybe I haven't come up with the right choice permutation yet.
    – user3732445
    Nov 13 at 9:57










  • Anyways, is it a good idea to mix javax.persistence.* org.hibernate.annotations.* annotations together?? GeneratedValue is javax and GenericGenerator is hibernate.
    – user3732445
    Nov 13 at 9:57
















Thank! I tried that and it didn't work, maybe I haven't come up with the right choice permutation yet.
– user3732445
Nov 13 at 9:57




Thank! I tried that and it didn't work, maybe I haven't come up with the right choice permutation yet.
– user3732445
Nov 13 at 9:57












Anyways, is it a good idea to mix javax.persistence.* org.hibernate.annotations.* annotations together?? GeneratedValue is javax and GenericGenerator is hibernate.
– user3732445
Nov 13 at 9:57




Anyways, is it a good idea to mix javax.persistence.* org.hibernate.annotations.* annotations together?? GeneratedValue is javax and GenericGenerator is hibernate.
– user3732445
Nov 13 at 9:57












up vote
0
down vote













As you say




I get an error from Postgres




If you check the docs it states:




Technically, a primary key constraint is simply a combination of a unique constraint and a not-null constraint.




That's also true for multi-column primary keys (see here)



So, if ts is part of your primary key in the database (as the @Id indicates) it's simply not possible to insert null values in that column.



IMO Hibernate/Spring got nothing to do with that as



insert into ms.time_series (ts, name, reading) values (NULL, 'sensor1', 10.0)


should be equivalent to



insert into ms.time_series (name, reading) values ('sensor1', 10.0)





share|improve this answer





















  • well if Hibernate/Spring have nothing to do with this, then the SQL query sent should be without the "ts" still. Also I said I tried to make the "ts" not part of the primary key and the error still persisted
    – user3732445
    Nov 13 at 19:43










  • CREATE TABLE ms.time_series ( ts TIMESTAMP(0) DEFAULT ('now'::text)::timestamp(0), name VARCHAR(255), reading DOUBLE PRECISION, PRIMARY KEY(ts, name) ); in SQL console I can run a query insert into ms.time_series (name, reading) values ('sensor1', 10) no problem
    – user3732445
    Nov 13 at 19:49










  • Why are you creating a timestamp with now, convert that to text to convert that to a timestamp again? What is the reasoning behind that.
    – M. Deinum
    Nov 13 at 19:50










  • @M.Deinum I copy pasted that from somewhere, I will fix it, but I don't believe it is the cause of the problem. The problem is that Spring/Hibernate is sending an incorrect SQL query which may be either caused by me improperly using the annotations or a bug in the framework itself.
    – user3732445
    Nov 13 at 19:57










  • It isn't an incorrect SQL, the SQL is perfectly valid. Spring has nothing to do with. The problem is the mapping. You want a non-generated ID and don't want to insert it. Hibernate has to make a decisions and decides that it needs to insert the currently assigned value. You need to tell hibernate that it should use a database generated value, else hibernate doesn't know how to handle it. It currently assumes client assigned/generated ids.
    – M. Deinum
    Nov 13 at 20:00















up vote
0
down vote













As you say




I get an error from Postgres




If you check the docs it states:




Technically, a primary key constraint is simply a combination of a unique constraint and a not-null constraint.




That's also true for multi-column primary keys (see here)



So, if ts is part of your primary key in the database (as the @Id indicates) it's simply not possible to insert null values in that column.



IMO Hibernate/Spring got nothing to do with that as



insert into ms.time_series (ts, name, reading) values (NULL, 'sensor1', 10.0)


should be equivalent to



insert into ms.time_series (name, reading) values ('sensor1', 10.0)





share|improve this answer





















  • well if Hibernate/Spring have nothing to do with this, then the SQL query sent should be without the "ts" still. Also I said I tried to make the "ts" not part of the primary key and the error still persisted
    – user3732445
    Nov 13 at 19:43










  • CREATE TABLE ms.time_series ( ts TIMESTAMP(0) DEFAULT ('now'::text)::timestamp(0), name VARCHAR(255), reading DOUBLE PRECISION, PRIMARY KEY(ts, name) ); in SQL console I can run a query insert into ms.time_series (name, reading) values ('sensor1', 10) no problem
    – user3732445
    Nov 13 at 19:49










  • Why are you creating a timestamp with now, convert that to text to convert that to a timestamp again? What is the reasoning behind that.
    – M. Deinum
    Nov 13 at 19:50










  • @M.Deinum I copy pasted that from somewhere, I will fix it, but I don't believe it is the cause of the problem. The problem is that Spring/Hibernate is sending an incorrect SQL query which may be either caused by me improperly using the annotations or a bug in the framework itself.
    – user3732445
    Nov 13 at 19:57










  • It isn't an incorrect SQL, the SQL is perfectly valid. Spring has nothing to do with. The problem is the mapping. You want a non-generated ID and don't want to insert it. Hibernate has to make a decisions and decides that it needs to insert the currently assigned value. You need to tell hibernate that it should use a database generated value, else hibernate doesn't know how to handle it. It currently assumes client assigned/generated ids.
    – M. Deinum
    Nov 13 at 20:00













up vote
0
down vote










up vote
0
down vote









As you say




I get an error from Postgres




If you check the docs it states:




Technically, a primary key constraint is simply a combination of a unique constraint and a not-null constraint.




That's also true for multi-column primary keys (see here)



So, if ts is part of your primary key in the database (as the @Id indicates) it's simply not possible to insert null values in that column.



IMO Hibernate/Spring got nothing to do with that as



insert into ms.time_series (ts, name, reading) values (NULL, 'sensor1', 10.0)


should be equivalent to



insert into ms.time_series (name, reading) values ('sensor1', 10.0)





share|improve this answer












As you say




I get an error from Postgres




If you check the docs it states:




Technically, a primary key constraint is simply a combination of a unique constraint and a not-null constraint.




That's also true for multi-column primary keys (see here)



So, if ts is part of your primary key in the database (as the @Id indicates) it's simply not possible to insert null values in that column.



IMO Hibernate/Spring got nothing to do with that as



insert into ms.time_series (ts, name, reading) values (NULL, 'sensor1', 10.0)


should be equivalent to



insert into ms.time_series (name, reading) values ('sensor1', 10.0)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 16:03









tom

1,0691412




1,0691412












  • well if Hibernate/Spring have nothing to do with this, then the SQL query sent should be without the "ts" still. Also I said I tried to make the "ts" not part of the primary key and the error still persisted
    – user3732445
    Nov 13 at 19:43










  • CREATE TABLE ms.time_series ( ts TIMESTAMP(0) DEFAULT ('now'::text)::timestamp(0), name VARCHAR(255), reading DOUBLE PRECISION, PRIMARY KEY(ts, name) ); in SQL console I can run a query insert into ms.time_series (name, reading) values ('sensor1', 10) no problem
    – user3732445
    Nov 13 at 19:49










  • Why are you creating a timestamp with now, convert that to text to convert that to a timestamp again? What is the reasoning behind that.
    – M. Deinum
    Nov 13 at 19:50










  • @M.Deinum I copy pasted that from somewhere, I will fix it, but I don't believe it is the cause of the problem. The problem is that Spring/Hibernate is sending an incorrect SQL query which may be either caused by me improperly using the annotations or a bug in the framework itself.
    – user3732445
    Nov 13 at 19:57










  • It isn't an incorrect SQL, the SQL is perfectly valid. Spring has nothing to do with. The problem is the mapping. You want a non-generated ID and don't want to insert it. Hibernate has to make a decisions and decides that it needs to insert the currently assigned value. You need to tell hibernate that it should use a database generated value, else hibernate doesn't know how to handle it. It currently assumes client assigned/generated ids.
    – M. Deinum
    Nov 13 at 20:00


















  • well if Hibernate/Spring have nothing to do with this, then the SQL query sent should be without the "ts" still. Also I said I tried to make the "ts" not part of the primary key and the error still persisted
    – user3732445
    Nov 13 at 19:43










  • CREATE TABLE ms.time_series ( ts TIMESTAMP(0) DEFAULT ('now'::text)::timestamp(0), name VARCHAR(255), reading DOUBLE PRECISION, PRIMARY KEY(ts, name) ); in SQL console I can run a query insert into ms.time_series (name, reading) values ('sensor1', 10) no problem
    – user3732445
    Nov 13 at 19:49










  • Why are you creating a timestamp with now, convert that to text to convert that to a timestamp again? What is the reasoning behind that.
    – M. Deinum
    Nov 13 at 19:50










  • @M.Deinum I copy pasted that from somewhere, I will fix it, but I don't believe it is the cause of the problem. The problem is that Spring/Hibernate is sending an incorrect SQL query which may be either caused by me improperly using the annotations or a bug in the framework itself.
    – user3732445
    Nov 13 at 19:57










  • It isn't an incorrect SQL, the SQL is perfectly valid. Spring has nothing to do with. The problem is the mapping. You want a non-generated ID and don't want to insert it. Hibernate has to make a decisions and decides that it needs to insert the currently assigned value. You need to tell hibernate that it should use a database generated value, else hibernate doesn't know how to handle it. It currently assumes client assigned/generated ids.
    – M. Deinum
    Nov 13 at 20:00
















well if Hibernate/Spring have nothing to do with this, then the SQL query sent should be without the "ts" still. Also I said I tried to make the "ts" not part of the primary key and the error still persisted
– user3732445
Nov 13 at 19:43




well if Hibernate/Spring have nothing to do with this, then the SQL query sent should be without the "ts" still. Also I said I tried to make the "ts" not part of the primary key and the error still persisted
– user3732445
Nov 13 at 19:43












CREATE TABLE ms.time_series ( ts TIMESTAMP(0) DEFAULT ('now'::text)::timestamp(0), name VARCHAR(255), reading DOUBLE PRECISION, PRIMARY KEY(ts, name) ); in SQL console I can run a query insert into ms.time_series (name, reading) values ('sensor1', 10) no problem
– user3732445
Nov 13 at 19:49




CREATE TABLE ms.time_series ( ts TIMESTAMP(0) DEFAULT ('now'::text)::timestamp(0), name VARCHAR(255), reading DOUBLE PRECISION, PRIMARY KEY(ts, name) ); in SQL console I can run a query insert into ms.time_series (name, reading) values ('sensor1', 10) no problem
– user3732445
Nov 13 at 19:49












Why are you creating a timestamp with now, convert that to text to convert that to a timestamp again? What is the reasoning behind that.
– M. Deinum
Nov 13 at 19:50




Why are you creating a timestamp with now, convert that to text to convert that to a timestamp again? What is the reasoning behind that.
– M. Deinum
Nov 13 at 19:50












@M.Deinum I copy pasted that from somewhere, I will fix it, but I don't believe it is the cause of the problem. The problem is that Spring/Hibernate is sending an incorrect SQL query which may be either caused by me improperly using the annotations or a bug in the framework itself.
– user3732445
Nov 13 at 19:57




@M.Deinum I copy pasted that from somewhere, I will fix it, but I don't believe it is the cause of the problem. The problem is that Spring/Hibernate is sending an incorrect SQL query which may be either caused by me improperly using the annotations or a bug in the framework itself.
– user3732445
Nov 13 at 19:57












It isn't an incorrect SQL, the SQL is perfectly valid. Spring has nothing to do with. The problem is the mapping. You want a non-generated ID and don't want to insert it. Hibernate has to make a decisions and decides that it needs to insert the currently assigned value. You need to tell hibernate that it should use a database generated value, else hibernate doesn't know how to handle it. It currently assumes client assigned/generated ids.
– M. Deinum
Nov 13 at 20:00




It isn't an incorrect SQL, the SQL is perfectly valid. Spring has nothing to do with. The problem is the mapping. You want a non-generated ID and don't want to insert it. Hibernate has to make a decisions and decides that it needs to insert the currently assigned value. You need to tell hibernate that it should use a database generated value, else hibernate doesn't know how to handle it. It currently assumes client assigned/generated ids.
– M. Deinum
Nov 13 at 20:00


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277820%2fjpa-annotation-columninsertable-false-is-being-ignored-why%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents