I want create two tables with Hibernate JPA
.
I use Oracle 11g
and JDBC 6 & 7
(Because I'm not sure which one is suitable for 11g :) ).
my library : hibernate-release-4.2.0.Final
& hibernate-jpa-2.0-api-1.0.1
& Tomcat-8 lib
& JDK 1.8.0-172
I create two Entities but when I run the program , the tables are not created and present this error :
Nov 12, 2018 10:15:35 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table "PICTURE" ("PID" `NUMBER` not null, " CAPTION " `LONG `, " LIKES_COUNTER " ` NUMBER `, " PIC_ADRESS " ` NVARCHAR2(50) ` not null, FK_USERS `NUMBER`, primary key ("PID"))
Nov 12, 2018 10:15:35 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: ORA-00911: invalid character
Nov 12, 2018 10:15:35 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table "USERS" ("UID" `NUMBER` not null, " EMAIL " ` NVARCHAR2(40)` not null, " USER_PASSWORD " ` NVARCHAR2(32)` not null, " USER_SEX " ` NVARCHAR2(20)`, " UPICADD " ` NVARCHAR2(50)`, " USERNAME " ` NVARCHAR2(30)` not null, primary key ("UID"))
Nov 12, 2018 10:15:35 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: ORA-00911: invalid character
I run these queries directly on SQL PLUS for testing. Tables were created correctly. even when I used <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
, The tables that I created directly , dropped but weren't rebuilt.
my entities :
@Entity (name = "person")
@Table(name = "USERS")
@EntityListeners(value = PersonManager.class)
public class Person implements Serializable
{
@Id
@Column(name="UID" ,columnDefinition = "NUMBER" )
@SequenceGenerator(name = "mySeq" , sequenceName = "DB_MYSEQ")
@GeneratedValue(strategy=GenerationType.AUTO ,generator="mySeq")
private long uId;
//--------define relation------
@OneToMany(cascade = CascadeType.ALL , fetch = FetchType.EAGER)
@JoinColumn(name = " FK_USERS" )
private List<Pictures> picturesList;
@Basic
@Column (name = " USERNAME " , columnDefinition = " NVARCHAR2(30)" , nullable = false )
private String username ;
@Basic
@Column (name = " USER_PASSWORD " , columnDefinition = " NVARCHAR2(32)" , nullable = false )
private String password ;
@Basic
@Column (name = " EMAIL " , columnDefinition = " NVARCHAR2(40)" , nullable = false)
private String email;
@Basic // user picture for profile
@Column (name = " UPICADD " , columnDefinition = " NVARCHAR2(50)" )
private String userPic;
@Basic
@Column (name = " USER_SEX " , columnDefinition = " NVARCHAR2(20)")
private String sex ;
public Person(String username, String password, String email ,String sex, String userPic ) {
this.picturesList = picturesList;
this.sex = sex;
this.userPic = userPic;
this.email = email;
this.password = password;
this.username = username;
}
//--------------------------------------------------------
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setEmail(String email) {
this.email = email;
}
public void setUserPic(String userPic) {
this.userPic = userPic;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setuId(long uId) {this.uId = uId;}
public void setPicturesList(List<Pictures> picturesList) {
this.picturesList = picturesList;
}
//--------------------------------------------------------
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getUserPic() {
return userPic;
}
public String getEmail() {
return email;
}
public String getUser_Sex() {
return sex;
}
public long getuId() {return uId;}
public List<Pictures> getPicturesList() {
return picturesList;
}
}
and
@Entity(name = "picture")
@Table(name = "PICTURE")
public class Pictures implements Serializable
{
@Id // create id and fill auto by sequence in database
@Column(name="PID" ,columnDefinition = "NUMBER" )
@SequenceGenerator(name = "mySeq2" , sequenceName = "DB_MYSEQ2")
@GeneratedValue(strategy=GenerationType.AUTO ,generator="mySeq2")
private long pId;
@Basic
@Column (name = "PICADRESS" , columnDefinition = "NVARCHAR2(50)" , nullable = false)
private String picAdress ;
@Basic
@Column (name = "CAPTION" , columnDefinition = "LONG")
private String caption;
@Basic
@Column (name = "LIKE_COUNTER" , columnDefinition = "NUMBER")
private int likes;
//--------------------------------------------------------
public Pictures(){}
public Pictures( String picAdress, String caption, int likes) {
this.picAdress = picAdress;
this.caption = caption;
this.likes = likes;
}
//--------------------------------------------------------
public void setPid(long pid) {
this.pId = pid;
}
public void setLikes(int likes) {
this.likes = likes;
}
public void setPicAdress(String picAdress) {
this.picAdress = picAdress;
}
public void setCaption(String caption) {
this.caption = caption;
}
//--------------------------------------------------------
public int getLikes() {
return likes;
}
public String getCaption() {
return caption;
}
public String getPicAdress() {
return picAdress;
}
public long getPid() {
return pId;
}
}
and persistence.xml :
<persistence-unit name="MyConnection" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.connection.username" value="midas"/>
<property name="hibernate.connection.password" value="midas123"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="show_sql" value="true"></property>
<property name="hibernate.globally_quoted_identifiers" value="true"/>
</properties>
</persistence-unit>
How can I fix it?
I finally discovered the problem. this property
<property name="hibernate.globally_quoted_identifiers" value="true"/>
in persistence.xml , add single Quotation for any data_type instead of name column!I deleted it and solved problem. :)