当我首先运行我的代码时它工作正常,但后来我无法访问它们 表?
这是我的代码: -
的hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/navlic</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="dto.UserDetails"/>
</session-factory>
</hibernate-configuration>
主要课程: -
package dto;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GenerationType;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateTest {
public static void main(String[] args) {
UserDetails ud=new UserDetails();
ud.setUserName("Second user");
Address homead=new Address();
homead.setCity("smg");
homead.setCountry("aa");
homead.setHouse("bb");
homead.setStreet("cc");
homead.setState("dd");
Address officead=new Address();
officead.setCity("smg");
officead.setCountry("ee");
officead.setHouse("ff");
officead.setStreet("gg");
officead.setState("hh");
ud.getAddr().add(homead);
ud.getAddr().add(officead);
SessionFactory sf;
ServiceRegistry sr;
Configuration cfg=new Configuration().configure();
sr=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
sf=cfg.buildSessionFactory(sr);
Session s=sf.openSession();
s.beginTransaction();
s.save(ud);
s.getTransaction().commit();
s.close();
ud=null;
s=sf.openSession();
//if type is lazy it returns only the variables of that class no more reference variables.
//if type is Eager it returns all the elements of user class
ud=(UserDetails)s.get(UserDetails.class, 1);
System.out.println(ud.getAddr().size());
}
}
模特课: -
@Entity
public class UserDetails {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int UserId;
private String UserName;
@ElementCollection(fetch=FetchType.EAGER)
@JoinTable(name="User_Address",joinColumns=@JoinColumn(name="User_Id"))
@GenericGenerator(name="hilo-gen" ,strategy="hilo")
@CollectionId(columns={@Column(name="AddressId")},generator="hilo-gen",type=@Type(type="long"))
private Collection<Address> addr=new ArrayList<Address>();
public Collection<Address> getAddr() {
return addr;
}
public void setAddr(Collection<Address> addr) {
this.addr = addr;
}
public int getUserId() {
return UserId;
}
public void setUserId(int userId) {
UserId = userId;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
}
地址类在这里: -
@Embeddable
public class Address {
@Column(name="Office_name")
private String House;
public String getHouse() {
return House;
}
public void setHouse(String house) {
House = house;
}
public String getStreet() {
return Street;
}
public void setStreet(String street) {
Street = street;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public String getCountry() {
return Country;
}
public void setCountry(String country) {
Country = country;
}
@Column(name="street_name")
private String Street;
@Column(name="city_name")
private String City;
@Column(name="state_name")
private String State;
@Column(name="country_name")
private String Country;
}
当我尝试运行我的代码时没有来自hibernate的错误但是遇到了mysql错误我是mysql的新手可以任何人解释我这个错误的原因以及如何解决它。
提前致谢