Spring / Hibernate / Oracle:ORA-02289序列不存在?

时间:2016-08-08 14:37:19

标签: java spring oracle hibernate

尝试将新对象插入Oracle表时获取ArrayList<ModelPrice> modelPriceArrayList = new ArrayList<ModelPrice>(); RecyclerView rv; AdapterPrice adapterClass; LinearLayoutManager llm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_static_data); rv = (RecyclerView) findViewById(R.id.rv); fill_data(); rv.setHasFixedSize(true); llm = new LinearLayoutManager(static_data.this); rv.setLayoutManager(llm); adapterClass = new AdapterPrice(static_data.this, modelPriceArrayList); rv.setAdapter(adapterClass); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.setting_data, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.a: Collections.sort(modelPriceArrayList, new Comparator<ModelPrice>() { public int compare(ModelPrice p1, ModelPrice p2) { return (p1.getName()).compareTo(p2.getName()); } }); rv.setHasFixedSize(true); llm = new LinearLayoutManager(static_data.this); rv.setLayoutManager(llm); adapterClass = new AdapterPrice(static_data.this, modelPriceArrayList); rv.setAdapter(adapterClass); return true; case R.id.z: Collections.sort(modelPriceArrayList, new Comparator<ModelPrice>() { public int compare(ModelPrice p1, ModelPrice p2) { return p2.getName().compareTo(p1.getName()); } }); rv.setHasFixedSize(true); llm = new LinearLayoutManager(static_data.this); rv.setLayoutManager(llm); adapterClass = new AdapterPrice(static_data.this, modelPriceArrayList); rv.setAdapter(adapterClass); return true; case R.id.l: Collections.sort(modelPriceArrayList, new Comparator<ModelPrice>() { @Override public int compare(ModelPrice p1, ModelPrice p2) { return (int) p1.getPrice() - p2.getPrice(); } }); rv.setHasFixedSize(true); llm = new LinearLayoutManager(static_data.this); rv.setLayoutManager(llm); adapterClass = new AdapterPrice(static_data.this, modelPriceArrayList); rv.setAdapter(adapterClass); return true; case R.id.h: Collections.sort(modelPriceArrayList, new Comparator<ModelPrice>() { @Override public int compare(ModelPrice p1, ModelPrice p2) { return (int) p2.getPrice() - p1.getPrice(); } }); rv.setHasFixedSize(true); llm = new LinearLayoutManager(static_data.this); rv.setLayoutManager(llm); adapterClass = new AdapterPrice(static_data.this, modelPriceArrayList); rv.setAdapter(adapterClass); return true; case R.id.New: final Dialog dialog = new Dialog(static_data.this); dialog.setContentView(R.layout.custom_dialog); dialog.setTitle("Blah Blah..."); final EditText textViewcolor = (EditText) dialog.findViewById(R.id.color_name1); final EditText textViewprice = (EditText) dialog.findViewById(R.id.color_price1); Button btnok = (Button) dialog.findViewById(R.id.btnOk); Button btncancle = (Button) dialog.findViewById(R.id.btncancle); btnok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name = String.valueOf(textViewcolor.getText()); String price = String.valueOf(textViewprice.getText()); modelPriceArrayList.add(new ModelPrice(name, Integer.parseInt(price))); adapterClass.notifyItemChanged(0); dialog.dismiss(); } }); btncancle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); dialog.show(); return true; default: return super.onOptionsItemSelected(item); } } void fill_data() { modelPriceArrayList.add(new ModelPrice("Red", 25)); modelPriceArrayList.add(new ModelPrice("white", 215)); modelPriceArrayList.add(new ModelPrice("Yello", 245)); modelPriceArrayList.add(new ModelPrice("Balck", 245)); modelPriceArrayList.add(new ModelPrice("DarkViolet", 275)); modelPriceArrayList.add(new ModelPrice("DeepPink", 225)); modelPriceArrayList.add(new ModelPrice("Fuchsia", 235)); modelPriceArrayList.add(new ModelPrice("Gold", 425)); modelPriceArrayList.add(new ModelPrice("Gray", 25)); modelPriceArrayList.add(new ModelPrice("HotPink", 725)); modelPriceArrayList.add(new ModelPrice("IndianRed", 525)); modelPriceArrayList.add(new ModelPrice("Indigo", 225)); modelPriceArrayList.add(new ModelPrice("Gainsboro", 3325)); modelPriceArrayList.add(new ModelPrice("Brown", 425)); modelPriceArrayList.add(new ModelPrice("Chartreuse", 725)); modelPriceArrayList.add(new ModelPrice("DarkCyan", 5)); modelPriceArrayList.add(new ModelPrice("DarkGrey", 225)); modelPriceArrayList.add(new ModelPrice("DarkSlateBlue", 285)); modelPriceArrayList.add(new ModelPrice("MintCream", 275)); modelPriceArrayList.add(new ModelPrice("PaleVioletRed", 825)); modelPriceArrayList.add(new ModelPrice("PaleVioletRed", 625)); } 。该表确实有一个序列,每个条目都会自动递增。

我已经坚持了几个小时,在对这个问题和其他文章做出类似的回答后,我仍然坚持。

我的课程:

    WHERE  route_ou = 2
    AND   (  (@l_s_query IS NOT NULL AND route_query = @l_s_query ) OR
              @l_s_query IS NULL ) 
    AND   lang_id  = 1

班上的DAO:

java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist

序列的DDL片段:

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.springframework.stereotype.Component;

@Entity
@Table(name = "MY_SCHEMA.MY_TABLE")
@Component
public class SomeClass {
    @Id
    @SequenceGenerator(name = "MY_SEQ", sequenceName = "MY_SEQ", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MY_SEQ")
    @Column(name = "MY_ID")
    private Integer myId;

    @Column(name = "MY_TS")
    private Timestamp ts;

    @Column(name = "MY_PARAM")
    private String myParameters;

    @Column(name = "ANOTHER_TS")
    private Timestamp anotherTimestamp;

    // empty constructor and getters/setters

}

hibernate.cfg.xml中:

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Component;

import mypackage.mysubpackage.SomeClass;

@Component
public class SomeClassDAO {

    private Session currentSession;
    private Transaction currentTransaction;

    private static SessionFactory getSessionFactory() {
        Configuration configuration = new Configuration().configure();
        configuration.addAnnotatedClass(SomeClass.class);
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());
        SessionFactory factory = configuration.buildSessionFactory(builder.build());
        return factory;
    }

    public Session openCurrentSession() {
        currentSession = getSessionFactory().openSession();
        return currentSession;
    }

    public Session openCurrentSessionWithTransaction() {
        currentSession = getSessionFactory().openSession();
        currentTransaction = currentSession.beginTransaction();
        return currentSession;
    }

    public void closeCurrentSession() {
        currentSession.close();
    }

    public void closeCurrentSessionWithTransaction() {
        currentTransaction.commit();
        currentSession.close();
    }

    public Session getCurrentSession() {
        return currentSession;
    }

    public void setCurrentSession(Session currentSession) {
        this.currentSession = currentSession;
    }

    // post
    public void insertNew() {
        SomeClass obj = new SomeClass();
        obj.setParameters("abc");
        getCurrentSession().save(obj);
    }

}

mvc-dispatchet-servlet.xml片段:

begin
   if inserting then
      if :NEW."MY_ID" is null then
         select MY_SEQ.nextval into :NEW."MY_ID" from dual;
      end if;
   end if;
end;

1 个答案:

答案 0 :(得分:2)

begin
   if inserting then
      if :NEW."MY_ID" is null then
         select MY_SEQ.nextval into :NEW."MY_ID" from dual;
      end if;
   end if;
end;

这看起来像是oracle trigger的一部分,而不是实际的oracle Sequence。检查序列是否实际存在,名称为&#34; MY_SEQ&#34;在您的架构中。

如果您的id列上有当前JPA注释的序列,则不需要触发器。 JPA本身可以在没有触发器的情况下获得序列的下一个值。

如果您仍想继续使用触发器,请阅读here