srikant nair

Ranch Hand
+ Follow
since Sep 09, 2017
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by srikant nair

Hi All,

This is a Basic JPA program of one to one mapping Here when i am running the program Primary key of Customer class i.e cid is 1 But ID of Address i.e aid and F.K sid is 2 I want aid and sid to be 1 instead of 2




My Customer Class





My Address Class
Hi All,

This is a Basic JPA program of one to one mapping Here when i am running the program Primary key of Customer class i.e cid is 1 But ID of Address i.e aid and F.K sid is 2 I want aid and sid to be 1 instead of 2

This is main class

public class Hibernate {

public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session se=sf.openSession();
Transaction tx=se.beginTransaction();
Customer c=new Customer();
c.setCname("srikant");
Address a=new Address();
a.setState("karnatka");
c.setAdd(a);
se.save(c);
tx.commit();
}}




@Entity
@Table(name="Cust")
public class Customer {

@Id
  @GeneratedValue
  @Column(name="cid")
  private int cid;

  private String cname;

  @OneToOne(cascade=CascadeType.ALL)
  @JoinColumn(name="sid") // Foreign Key
  private Address add;
  public int getCid() {
  return cid;
  }
  public void setCid(int cid) {
  this.cid = cid;
  }
  public String getCname() {
   return cname;
  }
    public void setCname(String cname) {
    this.cname = cname;
    }
    public Address getAdd() {
    return add;
    }
     public void setAdd(Address add) {
    this.add = add;
     }
    @Override
    public String toString() {
     return "Customer [cid=" + cid + ", cname=" + cname + ", add=" + add +
      "]";
}




  My Address Class

    @Entity
   @Table(name="Address")
   public class Address {

   private int aid;
   private String state;
   @Id
    @GeneratedValue
    public int getAid() {
    return aid;
     }
    public void setAid(int aid) {
    this.aid = aid;
   }
    public String getState() {
  return state;
     }
   public void setState(String state) {
    this.state = state;
   }

   @Override
   public String toString() {
    return "Address [aid=" + aid + ", state=" + state +  "]";
      }


         }
Hi
This is my hibernate code for one for one to many mapping


Customer has Requests


public class Customer {
private int id;  
private String qname;  
private Set<Request> answers;  
public Customer(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getQname() {
return qname;
}
public void setQname(String qname) {
this.qname = qname;
}
public Set<Request>getAnswers() {
return answers;
}
public void setAnswers(Set<Request>answers) {
this.answers = answers;
}


}


Request Pojo class


public class Request {
private int id;  
private String answername;  
private String postedBy;  
public Request(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAnswername() {
return answername;
}
public void setAnswername(String answername) {
this.answername = answername;
}
public String getPostedBy() {
return postedBy;
}
public void setPostedBy(String postedBy) {
this.postedBy = postedBy;
}

}


Mapping file of Customer


<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
   <hibernate-mapping package="com.srikant.hibernate3">
   <class name="Customer" table="cust">
   <id name="id" column="id">
   <generator class="increment"/>
   </id>
   <property name="qname" column="qname"/>
   <set name="answers" cascade="all">
   <key column="qid"/>
   <one-to-many class="Request"/>
   </set>
   </class>
   </hibernate-mapping>


Request mapping file


<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
   <hibernate-mapping package="com.srikant.hibernate3">
   <class name="Request" table="requests">
   <id name="id">  
         <generator class="increment"></generator>  
         </id>  
         <property name="answername"></property>  
         <property name="postedBy"></property>  
         </class>
         </hibernate-mapping>
           
   
   Main class

   
public class Hibernate {
public static void main(String[] args){
Configuration cfg=new Configuration();
   cfg.configure("hibernate.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session se=sf.openSession();
Transaction tx=se.beginTransaction();

System.out.println("configuration pass");
Request ans1=new Request();    
   ans1.setAnswername("Java is a programming language");    
   ans1.setPostedBy("Ravi Malik");    
       
   Request ans2=new Request();    
   ans2.setAnswername("Java is a platform");    
   ans2.setPostedBy("Sudhir Kumar");    
       
   Request ans3=new Request();    
   ans3.setAnswername("Servlet is an Interface");    
   ans3.setPostedBy("Jai Kumar");    
       
   Request ans4=new Request();    
   ans4.setAnswername("Servlet is an API");    
   ans4.setPostedBy("Arun");    
       
Set<Request> list1=new HashSet<Request>();    
   list1.add(ans1);    
   list1.add(ans2);    
       
   Set<Request> list2=new HashSet<Request>();    
   list2.add(ans3);    
   list2.add(ans4);    
       
   Customer question1=new Customer();    
   question1.setQname("What is Java?");    
   question1.setAnswers(list1);    
       
   Customer question2=new Customer();    
   question2.setQname("What is Servlet?");    
   question2.setAnswers(list2);    
       
   se.persist(question1);    
   se.persist(question2);    
       
   tx.commit();    
   se.close();    
   System.out.println("success");    
}  }  





As per my Request.hbm.xml file there are only three columns
ID
Answer NAME
Posted by

But
When i run my program
I get one more row in my ORacle


 QID which is the foreign key
Which i have not mentioned in in my request mapping file
 
I am creating a Hibernate application

package com.jlcindia.hibernate;
public class Customer {

private int cid;
private String cname;
private String emal;
private long phone;
private String city;
private double bal;

public Customer(){}
public Customer(String cname,String emal,long phone,String city,double bal){

this.cname=cname;
this.emal=emal;
this.phone=phone;
this.city=city;
this.bal=bal;

}


}


My application



package com.jlcindia.hibernate;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class Lab1A {

public static void main(String[] args) {
// TODO Auto-generated method stub
Transaction tx=null;
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

Session session = sessionFactory.openSession();
tx=session.beginTransaction();
Customer cust=new Customer("sri","sri@jlc.com",1234,"blore",25000.0);
session.save(cust);
tx.commit();
session.close();
System.out.println("record inserted");

}
catch(Exception e){
e.printStackTrace();

if(tx!=null)
tx.rollback();



}
}}



Configuraton file


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>

   <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
   <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
   <property name="username">system</property>
   <property name="password">Srikant1</property>
   <property name="dialect">org.hibernate.dialect.OracleDialect</property>
   <property name="show_sql">true</property>
   <mapping resource="com/jlcindia/hibernate/Customer.hbm.xml"/>
   </session-factory>
</hibernate-configuration>


Mapping file

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping package="com.jlc.india.hibernate">
<class name="Customer" table="customers">
<id name="cid" column="cid" type="int">
<generator class="increment"/>
</id>
<property name="cname"/>
<property name="emal" column="emal"/>
<property name="phone" column="phone" type="long"/>
<property name="city" column="city"   type="string"/>

<property name="bal" column="bal" type="double"/>
</class>
</hibernate-mapping>





But i am getting this error


SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2246)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at com.jlcindia.hibernate.Lab1A.main(Lab1A.java:11)
Caused by: org.dom4j.DocumentException: Error on line 30 of document  : The markup in the document following the root element must be well-formed. Nested exception: The markup in the document following the root element must be well-formed.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238)
... 3 more
class Test1{
public static void main(String jlc[]){
int a='7';
System.out.println(a);
}}


Please explain why the output is 55

5 years ago
No,

Still the same error
Is it because of wrong username or password ?
My SQL database is 5.5

Connector jar file is 8.0.11
When i commented
Class.forname




This is the error


java.sql.SQLNonTransientConnectionException: CLIENT_PLUGIN_AUTH is required
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:108)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:79)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at jdbcconnection.main(jdbcconnection.java:12)
Caused by: com.mysql.cj.exceptions.UnableToConnectException: CLIENT_PLUGIN_AUTH is required
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)
at com.mysql.cj.protocol.a.NativeAuthenticationProvider.connect(NativeAuthenticationProvider.java:220)
at com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1411)
at com.mysql.cj.NativeSession.connect(NativeSession.java:165)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:982)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
... 6 more
5 years ago
Hi ,

i am running a JDBC program in Eclipse




But when i run this program i am getting this error




How to remove this
5 years ago
So which equals method is being used in the above code
and why?
6 years ago
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Sri
{
   public static void main( String args[] )
   {
       Collection col = new ArrayList(); // DECLARE TYPE

       col.add( 10 );
       col.add( 39 );
       col.add( 50 );
       col.add( 60);
       
       System.out.println( col );
       Iterator it = col.iterator(); // DECLARE TYPE
       while( it.hasNext() )
       {
           Object obj = it.next(); // DECLARE TYPE
           System.out.println( obj );
           if( obj.equals( 10 ) )
           {
               it.remove();
           }
           else
           {
               System.out.println( col );
           }
       }
   }
}


If you look at this here also equals method is of String class
Is it by default objects are of String Type
6 years ago
The Object referenced by obj is a String, so the String equals is the method being called



How Did it convert to String Class
6 years ago
Sorry this is the output



[sri, nivas, dande, JLC]
sri
nivas
[nivas, dande, JLC]
dande
[nivas, dande, JLC]
JLC
[nivas, dande, JLC]
6 years ago
Hi,





In this object class equals method is executed .But that is meant for reference comparison
 it.remove statement gets executed


Why is this happening .I am confused



OutPut

[sri, nivas, dande, JLC]
sri
[sri, nivas, dande, JLC]
nivas
[sri, nivas, dande, JLC]
dande
[sri, nivas, dande, JLC]
JLC
[sri, nivas, dande, JLC]
6 years ago